1
votes

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)

1
Perhaps Doctrine Docs: Aggregation Builder can be of assistance. $dm is probably a DocumentManager instance in most examples.ccKep
Thanks for the answer but all the examples there start with:$builder = $dm->createAggregationBuilder(\Documents\User::class); So how can I instantiate that DocumentManager class? I thougth that was what $this->get('doctrine_mongodb')->getManager() did. It's probably me, who doesn't know enough of the doctrine way.mywebstuff.hq
@ccKep it would help a ton if you could please modify my examle code :)mywebstuff.hq
I never used mongodb, but $dm = $this->get('doctrine_mongodb')->getManager(); sounds about right. What does it return?ccKep
Yes, it is right as it turned out :) The problem was with my version (as posted in my answer), so the sample code above works just fine now :)mywebstuff.hq

1 Answers

1
votes

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.