doctrine2 findby two columns OR condition

后端 未结 2 2014
我寻月下人不归
我寻月下人不归 2020-12-11 19:10

My action:

   $matches_request = $em->getRepository(\'Bundle:ChanceMatch\')->findByRequestUser(1);
   $matches_reply = $em->getRepository(\'Bundle:C         


        
相关标签:
2条回答
  • 2020-12-11 19:49

    You can use QueryBuilder or create a custom repository for that entity and create a function that internally use QueryBuilder.

    $qb = $em->getRepository('FrontendChancesBundle:ChanceMatch')->createQueryBuilder('cm');
    $qb
        ->select('cm')
        ->where($qb->expr()->orX(
            $qb->expr()->eq('cm.requestUser', ':requestUser'),
            $qb->expr()->eq('cm.replyUser', ':replyUser')
        ))
        ->setParameter('requestUser', $requestUserId)
        ->setParameter('replyUser', $replyUserId)
    ;
    $matches_reply = $qb->getQuery()->getSingleResult();
    // $matches_reply = $qb->getQuery()->getResult(); // use this if there can be more than one result
    

    For more information on custom Repository see official documentation:

    http://symfony.com/doc/2.0/book/doctrine.html#custom-repository-classes

    0 讨论(0)
  • 2020-12-11 20:00

    It's possible to use Criteria for the complex queries with getRepository():

    $criteria = new \Doctrine\Common\Collections\Criteria();
    $criteria
      ->orWhere($criteria->expr()->contains('domains', 'a'))
      ->orWhere($criteria->expr()->contains('domains', 'b'));
    
    $domain->ages = $em
      ->getRepository('Group')
      ->matching($criteria);
    
    0 讨论(0)
提交回复
热议问题