简体   繁体   中英

How to build an Aggregation query in symfony with doctrine/mongodb?

I'm starting out with Symfony and doctrine/mongodb. I'm trying to do an aggregation query, but I get

Attempted to call an undefined method named "createAggregationBuilder" of class "Doctrine\\ODM\\MongoDB\\DocumentManager"

with something like this:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class TestController extends Controller
{
  /**
   * @Route("/test", name="test")
   */
  public function testAction(Request $request) {
    $builder = $this->get('doctrine_mongodb')
       ->getManager()
       ->createAggregationQuery('AppBundle:Test');

    var_dump($builder);
  }
}

I'm quite frustrated now, since every documentation or stackoverflow questions and answers start with

$dm->createAggregationQuery();

and I can't find out what $dm stands for in this case.

I'd really appreciate some help here.


UPDATE

I was looking around in the doctrine/mongodb-odm source code and I found that my version is missing the createAggregationBuilder function from both the DocumentRepository.php and the DocumentManager.php file (both located in /vendor/doctrine/mongodb-odm/lib/Doctrine/MongoDB folder.

How can this be?

I mean composer says that I have version 1.1.6, which is the latest release and on the git repo I can cleary see these methods ( DocumentRepository.php , DocumentManager.php )

Silly me. The problem was that I was using the 1.1.6 version of doctrine/mongodb-odm and the Aggregation Builder will only be available in version 1.2. Until than (for anyone being as blind as me), just use the "dev-master" version. In your composer.json modify the doctrine/mongodb-odm line to this:

"require": {
    "doctrine/mongodb-odm": "dev-master"
}

And then run composer update doctrine/mongodb-odm .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM