3

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.

Community
  • 1
  • 1

1 Answers1

1

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 function 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!

Tpojka
  • 6,435
  • 2
  • 25
  • 34
redbirdo
  • 4,747
  • 1
  • 27
  • 33
  • Thanks redbirdo, It works perfectly, I'm quite new to Symfony2 and Doctrine2 so was thinking more away from query. Since till now I have not been able to understand DQL syntax. – Saqib Mustafa Abbasi Mar 18 '15 at 02:03