2011-09-17 58 views
3

doctrine2 ODM可能創建以下查詢嗎?

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 }] }); 

這裏doctrine2的主要問題,我不明白的是如何添加另外$或$查詢?

這幫助我$和運營商仍然失蹤。

我目前使用的Symfony2 Doctrine2 MongoDB的

回答

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

除了理論中的一個小例子:

查詢在meta-language

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

查詢與學說的QueryBuilder

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