Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use entityManager inside Entity?

I have this function in Entity class but the getDoctrine do not fond...

public function getObject()
{
    $em = $this->getDoctrine()->getEntityManager();

    switch($this->objectType)
    {
        case 'video':
            return $em->getRepository('fdj2012AdminBundle:Video')->find($this->objectId);
            break;
        case 'default':
            return false;
            break;
    }
}

How to use entityManager inside my Entity ?

like image 611
Antoine Lenoir Avatar asked Jan 17 '23 09:01

Antoine Lenoir


1 Answers

Actually Entity shouldn't know about EM. I use Event Listeners if I need advance logic in my Entity. When you register Listeners like services you can pass args there, like a EM or Container and get them inside Listener class.

Symfony Doc

But I know not really good way to get EM inside Entity class. By taking global variable Kernel in Entity methods.

global $kernel;
if ( 'AppCache' == get_class($kernel) )
{
   $kernel = $kernel->getKernel();
}
$em = $kernel->getContainer()->get( 'doctrine.orm.entity_manager' );

Shame on me :(

like image 117
sean662 Avatar answered Jan 25 '23 14:01

sean662