1

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)

5
  • 2
    Perhaps Doctrine Docs: Aggregation Builder can be of assistance. $dm is probably a DocumentManager instance in most examples. Commented Oct 3, 2017 at 21:07
  • 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. Commented Oct 4, 2017 at 14:56
  • @ccKep it would help a ton if you could please modify my examle code :) Commented Oct 4, 2017 at 15:03
  • I never used mongodb, but $dm = $this->get('doctrine_mongodb')->getManager(); sounds about right. What does it return? Commented Oct 4, 2017 at 20:49
  • 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 :) Commented Oct 5, 2017 at 15:55

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.