symfony - 学说2 通过两列或条件查找

标签 symfony doctrine-orm symfony-2.1

我的行动:

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

是否可以将带有 or 条件的查询与 getRepository 连接起来,例如。

$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`. 

我的 table

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

我的查询应返回id 12 & 13

感谢您的帮助!

最佳答案

您可以使用 QueryBuilder 或为该实体创建自定义存储库并创建内部使用 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

有关自定义存储库的更多信息,请参阅官方文档:

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

关于symfony - 学说2 通过两列或条件查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15229609/

相关文章:

postgresql - 让 Doctrine 2 ORM 通过针对具有连接的表的自定义查询来混合对象

php - Symfony2 - 使用没有附加任何实体的表单生成器

mongodb - Doctrine ODM : How to select Mongo DB database

php - 左加入学说 2

mysql - 在 Doctrine2 的选择中使用比较作为别名

symfony - 何时应在 SonataAdmin 工作流程中禁用 Doctrine 过滤器?

symfony - Doctrine 和 Symfony : magic methods and caching

php - 跟踪 Doctrine 实体上的字段变化

java - GWT + symfony2,我疯了吗?

php - 使用 loggable 来引用订单行中的产品版本?