【问题标题】:How to create a WHERE ... OR ... query with MongoDB in Symfony2? [duplicate]如何在 Symfony2 中使用 MongoDB 创建 WHERE ... OR ... 查询? [复制]
【发布时间】:2012-10-04 04:09:12
【问题描述】:

在我的 UserRepository 中,我想创建自定义查询,就像我可以在不使用 MongoDB 时使用 $dm->createQuery('some query') 创建的查询一样。

我该怎么做?我看到$this->createQueryBuilder() 方法存在,但$this->createQuery() 不存在。

我也试过这个,因为它是有意义的,但没有奏效:

$this->createQueryBuilder('u')
    ->where(array('$or' => array(
        array('u.username' => $username),
        array('u.email' => $username)
    )))
    // ...

它说 $or 是一个无效的运算符。

【问题讨论】:

    标签: mongodb symfony doctrine-orm symfony-2.1 doctrine-odm


    【解决方案1】:

    在这里找到它:Doctrine2 Mongodb adding more $or operator

    /**
     * Adds an "or" expression to the current query.
     *
     * You can create the expression using the expr() method:
     *
     *     $qb = $this->createQueryBuilder('User');
     *     $qb
     *         ->addOr($qb->expr()->field('first_name')->equals('Kris'))
     *         ->addOr($qb->expr()->field('first_name')->equals('Chris'));
     *
     * @param array|QueryBuilder $expression
     * @return Builder
     */
    
    
    
    /**
     * Adds an "and" expression to the current query.
     *
     * You can create the expression using the expr() method:
     *
     *     $qb = $this->createQueryBuilder('User');
     *     $qb
     *         ->addAnd($qb->expr()->field('first_name')->equals('Kris'))
     *         ->addAnd($qb->expr()->field('first_name')->equals('Chris'));
     *
     * @param array|QueryBuilder $expression
     * @return Query
     */
    

    【讨论】:

      猜你喜欢
      • 2013-07-07
      • 2019-10-03
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多