John
John

Reputation: 1665

Symfony2 injecting entityManager error

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.

Upvotes: 0

Views: 1186

Answers (2)

Tomasz Madeyski
Tomasz Madeyski

Reputation: 10900

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

Upvotes: 3

scoolnico
scoolnico

Reputation: 3125

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.

Upvotes: 3

Related Questions