Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

symfony - doctrine2 findby two columns OR condition

My action:

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

Is it possible to join the querys with an or condition with getRepository, eg.

$matches_reply = $em->getRepository('FrontendChancesBundle:ChanceMatch')->findBy(array('requestUser' => 1, 'replyUser' => 1); 
//this of course gives me the a result when `requestUser` and `replyUser` is `1`. 

My table

id | requestUser | replyUser
....
12 |      1      |     2
13 |      5      |     1

My query should return the id 12 & 13.

Thanks for help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...