Reputation: 742
Is there in Doctrine2 any ways to find pack of entities by array of composite primary keys?
$primaryKeys = [
['key1'=>'val11', 'key2'=>'val21'],
['key1'=>'val12', 'key2'=>'val22']
];
Upvotes: 1
Views: 1574
Reputation: 44326
You will have to add your custom method to a repository. For example:
$repository = $em->getRepository('Application\Entity\Class');
$repository->findByPrimaryKeys();
And in your Application\Entity\Class
repository:
/**
* Find entity by primary keys.
*
* @param Parameters[] $array
* @return Paginator
*/
public function findByPrimaryKeys(array $array)
{
$qb = $this->createQueryBuilder('e');
foreach($array as $index => $keys){
$key1 = $index * 2 + 1;
$key2 = $index * 2 + 2;
$qb->orWhere(
$qb->expr()->andX(
$qb->expr()->eq('e.key1', '?'.$key1),
$qb->expr()->eq('e.key2', '?'.$key2)
)
);
$qb->setParameter($key1, $keys['key1']);
$qb->setParameter($key2, $keys['key2']);
};
return $qb->getQuery()->getResult();
}
Maybe there are other ways, but this works and results in following DQL query:
SELECT e FROM Application\Entity\Class e WHERE
(e.key1= ?1 AND e.key2= ?2)
OR
(e.key1= ?3 AND e.key2 = ?4)
Upvotes: 1