0

I have a controller class HomeController with a particular method to get the Entity Manager.:

protected function getImageManager() 
{
    if(!isset($_SESSION)) session_start();

    if(isset($_SESSION['id'])) 
    {                   
        if($em = $this->getDoctrine()->getEntityManager()) return $em;
    } 
    return false;
}

and it works when I use it inside HomeController. But when I extend the controller and try to use this method I get an error: Fatal error: Call to a member function has() on a non-object in /home/xxxx/webDir/project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 191

This is how I use this method inside HomeController and the extended controller:

if($em = $this->getImageManager())
...

This is how I extend HomeController:

namespace MSD\HomeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use MSD\HomeBundle\Entity\Imagen as Imagen;
use MSD\HomeBundle\Controller\HomeController as HomeController;

class ImageTransController extends HomeController
{
function __construct()
{
    if($em = $this->getImageManager()) return $this;
        else $this->setError('Entity Manager error');
    }
}

Any idea on what is going wrong?

Manolo
  • 24,020
  • 20
  • 85
  • 130

1 Answers1

0

You are doing a lot of things wrong here.

  1. session is started automatically in Symfony so you don't have to start it manually
  2. Why are you calling getImageManager when what you want is EntityManager?
  3. Why are you overriding the constructor?

This is how you should do it

// HomeController.php
public function getEntityManager()
{
    return $this->getDoctrine()->getEntityManager();
}

// ImageTransController
class ImageTransController extends HomeController
{
    public function someAction()
    {
        $em = $this->getEntityManager();
        // do something with $em
    }
}
praxmatig
  • 263
  • 3
  • 10
  • Now I know that the problem is to call Entity Manager in the constructor. You can have a look at my newer question: http://stackoverflow.com/questions/20587354/how-to-call-entity-manager-in-a-constructor – Manolo Dec 16 '13 at 11:06