如何使用我的实体和实体经理在Symfony 2控制台命令? [英] How to use my Entities and Entity Managers in Symfony 2 Console Command?

查看:206
本文介绍了如何使用我的实体和实体经理在Symfony 2控制台命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一些终端命令到我的Symfony2应用程序。我已阅读了食谱中的示例,但我找不到如何访问我的设置,我的实体经理和我的实体在这里。在构造函数中,我使用

I want to a few terminal commands to my Symfony2 application. I've gone through the example in the cookbook, but I couldn't find out how to access my settings, my entity manager and my entities here. In the constructor, I get the container (which should yield me access to settings and entities) using

$this->container = $this->getContainer();

但此调用会产生错误:


致命错误:调用/ Users / fester / Sites / thinkblue / admintool / vendor / symfony / src / Symfony / Bundle / FrameworkBundle / Command中的非对象上的成员函数getKernel ContainerAwareCommand.php on line 38

Fatal error: Call to a member function getKernel() on a non-object in /Users/fester/Sites/thinkblue/admintool/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand.php on line 38

基本上,在ContainerAwareCommand-> getContainer()中调用

Basically, in ContainerAwareCommand->getContainer() the call to

$this->getApplication()

返回NULL,而不是预期的对象。我想我离开了一些重要的步骤,但哪一个?

returns NULL and not an object as expected. I guess that I left some important step out, but which one? And how will I finally be able to use my settings and entities?

推荐答案

我认为你不应该在构造函数中检索容器直。而是在 configure 方法或 execute 方法中检索它。在我的case,我得到我的实体管理器只是在执行方法的开头像这样,一切工作正常(用Symfony 2.1测试)。

I think you should not retrieve the container in the constructor directly. Instead, retrieve it in the configure method or in the execute method. In my case, I get my entity manager just at the start of the execute method like this and everything is working fine (tested with Symfony 2.1).

protected function execute(InputInterface $input, OutputInterface $output)
{
    $entityManager = $this->getContainer()->get('doctrine')->getEntityManager();

    // Code here
}

当您在构造函数中调用 getContainer 时导致此错误的应用程序对象的实例化尚未完成。错误来自 getContainer 方法执行:

I think that the instantiation of the application object is not done yet when you are calling getContainer in your constructor which result in this error. The error comes from the getContainer method tyring to do:

$this->container = $this->getApplication()->getKernel()->getContainer();

由于 getApplication 你会得到一个错误说或正在调用一个方法 getKernel 在一个非对象。

Since getApplication is not an object yet, you get the a error saying or are calling a method getKernel on a non-object.

希望它有帮助。

感谢,

Matt

Regards,
Matt

更新:较新版本的Symfony, getEntityManager 已被弃用(现在可能已被删除)。而是使用 $ entityManager = $ this-> getContainer() - > get('doctrine') - > getManager(); 感谢 Chausser 指导。

Update: In newer version of Symfony, getEntityManager has been deprecated (and could have been removed altogether by now). Use $entityManager = $this->getContainer()->get('doctrine')->getManager(); instead. Thanks to Chausser for pointing it.

这篇关于如何使用我的实体和实体经理在Symfony 2控制台命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆