dongqiyang3711 2015-03-17 18:39 采纳率: 100%
浏览 43
已采纳

从ArrayCollection中提取ID以与ID数组进行比较

Good day,

I have a class Cars and class CarTypes in a Many-To-Many relation, Cars contain an ArrayCollection of CarTypes. I want to search for Cars satisfying any of multiple selected CarTypes (passed from form in array).

Till now I can only think of three nested loops, 1st over Cars, 2nd inner loop over ids array (from form) and 3rd inner loop on ArrayCollection of CarTypes in Cars to check if value from 2nd loop exists in 3rd.

I have already checked below links related to closure. But these or none of the links in docs provide sufficient details on how to match an array of selected ids against ids from ArrayCollection.

Doctrine 2 ArrayCollection filter method

Doctrine 2, query inside entities

I have Custom Repositories but feel it better to implement this in Cars class using exists of ArrayCollection, maybe something like following:

public function existsCarTypes($ctArray)
{
    $CarTypes = $this->CarTypes;
    return $CarTypes->exists( 
            function($CarTypes) use ($ctArray) 
            {
                if (in_array($ctArray , $CarTypes->getId() )) 
                {
                    return true;
                }
                return false;
            }
        );
}

But this is not working, Is there a better way to do this, please provide any good documentation links.

  • 写回答

1条回答 默认 最新

  • dsafq2131321 2015-03-17 22:33
    关注

    With some other language or architecture it might make sense to implement a Car search as a static method on the Car class. However, the way the doctrine ORM works the right place to implement an entity search is in a custom repository class.

    There may be a better way to do a search like this but this is the way I've done it in the past (I'm assuming you know how to add a custom repository to an entity, since you mention them in your question):

    class CarRepository extends EntityRepository
    { 
        public method findByCarTypes(array $carTypeIds)
        {
            if (!count($carTypeIds)) { return new ArrayCollection(); }
    
            $carTypesCondition = '';
            $parameters = array();
    
            for($i = 1; $i <= count($carTypeIds); $i++)
            {
                if (!empty($carTypesCondition)) { $carTypesCondition .= ' OR '; }
                $carTypesCondition .= 'carType.id = :carTypeId' . $i;
                $parameters['carTypeId' . $i] = $carTypeIds[$i-1];
            }
    
            $queryBuilder = $this->createQueryBuilder('car')
                ->innerJoin('car.carTypes', 'carType', 'WITH', $carTypesCondition);
                ->setParameters($parameters);
    
            $query = $queryBuilder->getQuery();
            $cars = $query->getResult();
    
            return $cars;
        }
    }
    

    NB - I extracted this code from a more complex method so no guarantees that it is error free!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP