在Doctrine2中,有没有办法通过组合主键的数组来查找实体包?
$primaryKeys = [
['key1'=>'val11', 'key2'=>'val21'],
['key1'=>'val12', 'key2'=>'val22']
];发布于 2015-12-18 22:08:00
您必须将您的自定义方法添加到存储库。例如:
$repository = $em->getRepository('Application\Entity\Class');
$repository->findByPrimaryKeys();在您的Application\Entity\Class存储库中:
/**
* 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();
}也许还有其他方法,但这是可行的,并导致以下DQL查询:
SELECT e FROM Application\Entity\Class e WHERE
(e.key1= ?1 AND e.key2= ?2)
OR
(e.key1= ?3 AND e.key2 = ?4)https://stackoverflow.com/questions/34356389
复制相似问题