Doctrine orm 无法访问命令行或控制台应用程序中的控制器功能
我正在准备一个控制台应用程序,并使用EntityManager的容器连接到数据库。这个很好用。代码如下Doctrine orm 无法访问命令行或控制台应用程序中的控制器功能,doctrine-orm,console-application,symfony-2.6,Doctrine Orm,Console Application,Symfony 2.6,我正在准备一个控制台应用程序,并使用EntityManager的容器连接到数据库。这个很好用。代码如下 protected function execute(InputInterface $input, OutputInterface $output) { // get Doctrine $this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); // find all d
protected function execute(InputInterface $input, OutputInterface $output)
{
// get Doctrine
$this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
// find all disc that are not treated by Manager in last week i.e sysdate - 6
$pendingApprovals = $this->em->getRepository('GMRestBundle:TDtlsDisc')->getPendingManagerAppoval();
// for each unapproved disc, start sending emails
$repository = $this->em->getRepository('GMRestBundle:TDtlsDisc');
$emailReminder = new SubmitDisclosureController();
foreach($pendingApprovals as $labManagerReview) {
$tDtlsDiscEntity = $repository->findByDiscId($labManagerReview['DISC_ID']);
$emailReminder->sendMailToLabManager($tDtlsDiscEntity);
}
}
现在的问题是,当我调用thsi$emailrementer->sendMailToLabManager($tDtlsDiscEntity)时,它给出了一个错误代码>这将依次连接orm并从数据库中获取一些数据。此sendMailToLabManager
位于SubmitDisclosureController
中,代码如下
public function sendMailToLabManager($tDtlsDiscEntity)
{
$repository = $this->getDoctrine()->getRepository('GMRestBundle:TXrefDiscSso');
$entityRepository = $this->getDoctrine()->getRepository('GMRestBundle:TDtlsEntity');
$discId = $tDtlsDiscEntity->getDiscId();
.......
在控制台应用程序中,我无法使用任何通过与doctrine对象建立另一个连接来访问DB的控制器。如果我通过web中的另一个动作控制器调用同一个控制器,它工作得很好
Error: Call to a member function has() on null
我现在看到上面的错误消息。您应该创建一个提供该功能的服务,而不是使用控制器。之后,您必须通过依赖项注入将EntityManager
插入到服务中
如果您的命令扩展了ContainerWareCommand
,您可以在命令中使用$this->get()
来检索您以前定义的服务。$emailReminder->setContainer($this->getContainer())将使您跳过错误消息,但不应直接调用控制器方法。将EmailReminder设置为独立服务,然后与命令和控制器共享:了解服务是有效使用Symfony框架的关键部分。