Doctrine2 Mongodb adding more $or operator

十年热恋 提交于 2019-12-30 07:16:07

问题


It is possible for doctrine2 ODM to create the following query?

db.Product.find({ "$or": [ { "name": new RegExp("test*", "i") }, { "tags": new RegExp("public true*", "i") } ], "$or": [{ "public": false, "_id": { "$in": [ ObjectId("4e74121c4fcfa9ff7ac90000"), ObjectId("4e74121c4fcfa9ff7ac80000") ] } }, { "public": true }] });

The main issue here with doctrine2 that I dont understand is how to add addition $or in the $query?

This help me with $and operator which is still missing.

I'm currently using Symfony2 Doctrine2 Mongodb


回答1:


/**
 * 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
 */



回答2:


A small example in addition to theory:

Query in meta-language:

<condition1> AND (<field1 == value1> OR <field2 == value2>)

Query with Doctrine's QueryBuilder:

$queryBuilder
    ->addAnd(<condition1>)
    ->addAnd(
        $queryBuilder
            ->expr()
            ->addOr(
                $queryBuilder
                    ->expr()
                    ->field('field1')
                    ->equals('value1')
            )->addOr(
                $queryBuilder
                    ->expr()
                    ->field('field2')
                    ->equals('value2')
            )
    );


来源:https://stackoverflow.com/questions/7452536/doctrine2-mongodb-adding-more-or-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!