Doctrine MongoDB querybuilder组

时间:2013-12-17 13:57:20

标签: mongodb doctrine query-builder

我尝试将mongo查询翻译为Doctrine。我是Doctrine和PHP的新手,我无法将我的查询翻译成学说。

我已被封锁超过两天了!

我无法理解官方参考资料

http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/query-builder-api.html

任何人都可以帮助我!

查询是一个:

db.prices.group( {
    key: { product: 1, category:1 },
cond: { product: "1" },
    reduce: function ( curr, result ) {
       result.total += curr.amount;
    },
    initial: { total : 0 }
   } )

文件如下:

 {
  "_id" : ObjectId("52af22231852fcf313b49375"),
  "product" : DBRef("products", ObjectId("529d8db11852fc7a2ac182ac")),
  "category" : "1",
  "date" : ISODate("2013-12-16T15:54:11Z"),
  "price" : 51
 }

谢谢!

1 个答案:

答案 0 :(得分:2)

我找到了解决方案,

 $qm = $this->dm->createQueryBuilder ( '\model\entity \Product' )->group ( array (
      'product' => 1 
 ), array (
      'total' => 0 
 ) )->reduce ( 'function ( curr, result ) { result.total += curr.price;}' );

 $qm->field ( 'category' )->equals ( $idCategory );

 $result = $qm->getQuery ()->execute ();