如何在slmqueue工作中获得doctrine2 entitymanager?

时间:2014-07-16 18:52:14

标签: zend-framework doctrine-orm

我将使用SlmQueue(https://github.com/juriansluiman/SlmQueueDoctrine)。

如何在作业班内获得doctrine2 entitymanager?

2 个答案:

答案 0 :(得分:0)

我设法做到这一点:

        class DistributeNewsJob extends AbstractJob implements QueueAwareInterface, ServiceLocatorAwareInterface
        {   
            use QueueAwareTrait;

            public function execute()
            {
                 // job code

            }

            private $entityManager;
            private function getEntityManager()
            {
                if (null === $this->entityManager) {    
                    $this->entityManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
                }
                return $this->entityManager;
            }

            private $services;

            public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
            {
                $this->services = $serviceLocator->getServiceLocator();
            }

            public function getServiceLocator()
            {
                return $this->services;
            }

            public function dispatch(Request $request, Response $response = null)
            {
            }
        }

答案 1 :(得分:0)

我们也可以这样做:

您可以使用doctrine module

中的ObjectManagerAwareInterface
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use DoctrineModule\Persistence\ProvidesObjectManager as ProvidesObjectManagerTrait;

class EmailJob extends AbstractJob implements ObjectManagerAwareInterface
{
    use ProvidesObjectManagerTrait;
}

这样你就可以在你的工作中使用ObjectManager。