6

I have a ArrayCollection of status objects that I would now like to use as the IN parameter in a WHERE clause for a doctrine query. Here's my query code:

$query = $repository->createQueryBuilder('r')
                ->join('r.applicationStatus', 's')
                ->where('r.submitted IS NOT NULL')
                ->andWhere('r.created >= :date')                  
                ->andWhere('r.created < :date2')
                ->andWhere('s IN (:status)') // Here's the In statement
                ->orderBy('r.created', 'DESC')                
                ->setParameter('date', $appSearch->getDateFrom())
                ->setParameter('date2', $end)
                ->setParameter('status', $appSearch->getApplicationStatus()) //Here's the array collection
                ->getQuery();

However the query is returning zero records. For it to work I have to manually iterate through the $appSearch->getApplicationStatus() arraycollection and grab the status id's in a new array for the query to yield correct results at the moment - which feels very inefficient.

What am I doing wrong?

2 Answers 2

10

You should do something like that:

$statusId = array();

foreach ($appSearch->getApplicationStatus() as $status) {
    $statusId[] = $status->getId();
}

// ... Your query:
->andWhere('s.id IN (:status)')
->setParameter('status', $statusId)

In fact I think that Doctrine can't filter something by giving an object, it is not capable to compare it, so you need to use a field of these objects, here the id in my example... and comparing integer is lighter for your database !

Sign up to request clarification or add additional context in comments.

2 Comments

ArrayCollection class has the (very convenient) toArray method to do the same thing.
It's a shame, since Doctrine can accept an object as a parameter for an eq() comparison, but doesn't seem to like an array of objects.
0

its work fine in contrioller .

$users =  $em->getRepository('EventekUserBundle:User')->getQueryf($events->getSecretariats()->toArray()) ;

in repositroy =>

class UserRepository extends \Doctrine\ORM\EntityRepository
{

    public function getQueryFromCollection( $users)
    {
        $qb = $this->createQueryBuilder('u')
            ->select('u')
            ->andWhere('u in (:users)')
            ->setParameter('users',  $users);
        return $qb->getQuery();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.