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 ?
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 :(
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With