2015-12-18 76 views

回答

1

您將不得不將自定義方法添加到存儲庫。例如:

$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)