0
votes

I try to translate a mongo query to Doctrine. I am new with Doctrine and PHP and I am unable to translate my query to doctrine.

I have been blocked for over two days now!

I can't understand the official references

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

Can anybody help me!

The query is that one:

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

The documents look like:

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

Thank you!!

1

1 Answers

2
votes

I found the solution,

 $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 ();