0

I am following a Doctrine ORM Symfony2 Documentation. When it comes to Persisting Objects to the Database I get this error:

Attempted to call an undefined method named "getDoctrine" of class "BooksApi\BookBundle\Controller\IndexController".

The only thing I am doing differently in my code is that I am trying to create EntityManager as a service....

services.xml:

<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="booksapi.controller.index"
                 class="BooksApi\BookBundle\Controller\IndexController">
            <argument type="service" id="booksapi.repositories.test_repository" />
            <argument type="service" id="doctrine.orm.entity_manager" />
        </service>
    </services>
</container> 

my Index Controller:

<?php

namespace BooksApi\BookBundle\Controller;

use BooksApi\BookBundle\Entity\BooksEntity;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Response;


class IndexController
{
    /**
     * @var EntityManager
     */
    public $em;

    /**
     *  @param EntityManager $entityManager
     */
    public function __construct(
        EntityManager $entityManager
    ){
        $this->em = $entityManager;
    }

    /**
     * @return Response
     */
    public function testAction()
    {

        $book = new BooksEntity();
        $book->setTitle('Tomazi in da Jungle');
        $book->setPrice('19.99');
        $book->setDescription('Lorem ipsum dolor');

        $this->em = $this->getDoctrine()->getManager();

        $this->em->persist($book);
        $this->em->flush();

        return new Response('Created product id '.$book->getId());
    }
}

So looking at the error getDoctrine method is not recognised....any idea why...? How can I fix this.

1
  • You are missing the obvious. $this->em is set in your constructor so remove $this->em = $this->getDoctrine()->getManager(); and all will be well. Commented Jan 19, 2016 at 15:06

2 Answers 2

3

1/ Quick solution:

Remove this line: $this->em = $this->getDoctrine()->getManager();

2/ Better solution:

IndexController should extend Controller

(Symfony\Bundle\FrameworkBundle\Controller\Controller)

And getDoctrine method could be available. By this way, Doctrine Entity Manager don't need to be injected. No constructor, no service definition.

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

3 Comments

Is there any way around than extending a Controller...?
---> Remove this line: $this->em = $this->getDoctrine()->getManager(); <---
Yep, as you do: Injected Doctrine entity manager. So, use the first option I suggest.
3

Defining your controller is a good practice so I would stick with that. Two things here seem wrong:

  1. Your service definition (services.xml) contains two parameters and your Controller constructor accepts only one argument.

  2. This line: $this->em = $this->getDoctrine()->getManager(); : you don't need it at all since your $this->em is already defined in constructor and its value is an EntityManager instance. Just remove this line and you should be good

And the reason you get this error is just because you are trying to use getDoctrine method which is a Controller method. What it does is just asking Container to create instance of EntityManager and since you have this instance already injected into constructor this call (getDoctrine) is not needed at all

1 Comment

Perfect this works. Thank you for the answer. Unfortunately Scoolnico answered first so will accept his answer but up voted yours as well.

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.