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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程