1
Doctrine2中是否有任何方式通過複合主鍵數組來找到實體包?學說通過複合主鍵數組找到多個
$primaryKeys = [
['key1'=>'val11', 'key2'=>'val21'],
['key1'=>'val12', 'key2'=>'val22']
];
Doctrine2中是否有任何方式通過複合主鍵數組來找到實體包?學說通過複合主鍵數組找到多個
$primaryKeys = [
['key1'=>'val11', 'key2'=>'val21'],
['key1'=>'val12', 'key2'=>'val22']
];
您將不得不將自定義方法添加到存儲庫。例如:
$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)