如何在 Symfony 2 控制台命令中使用我的实体和实体管理器?
我想要对我的 Symfony2 应用程序执行一些终端命令。我已经浏览了食谱中的示例,但我不知道如何访问我的设置,我的实体管理器和我的实体在这里。在构造函数中,我使用以下方法获取容器(这应该让我能够访问设置和实体)
$this->container = $this->getContainer();
但是此调用会生成错误:
致命错误:在线调用 /Users/fester/Sites/thinkblue/admintool/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand.php 中非对象的成员函数 getKernel() 38
基本上,在 ContainerAwareCommand->getContainer() 中,调用
$this->getApplication()
返回 NULL,而不是预期的对象。我想我遗漏了一些重要的步骤,但是哪一个呢?我最终将如何使用我的设置和实体?
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();
But this call generates an error:
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
Basically, in ContainerAwareCommand->getContainer() the call to
$this->getApplication()
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你不应该直接在构造函数中检索容器。相反,请在
configure
方法或execute
方法中检索它。就我而言,我在execute
方法的开头就得到了我的实体管理器,就像这样,一切都工作正常(使用 Symfony 2.1 进行了测试)。我认为当您在构造函数中调用 getContainer 时,应用程序对象的实例化尚未完成,从而导致此错误。该错误来自
getContainer
方法尝试执行的操作:由于
getApplication
还不是对象,因此您会收到错误消息,说明或正在调用方法getKernel
code> 在非对象上。更新:在较新版本的 Symfony 中,
getEntityManager
已被弃用(现在可能已被完全删除)。请改用$entityManager = $this->getContainer()->get('doctrine')->getManager();
。感谢 Chausser 的指出。更新 2:在 Symfony 4 中,可以使用自动装配来减少所需的代码量。
使用 EntityManagerInterface 变量创建一个 __constructor 。该变量将可以在其余命令中访问。这遵循自动装配依赖注入方案。
感谢 @profm2 提供的评论和代码示例。
I think you should not retrieve the container in the constructor directly. Instead, retrieve it in the
configure
method or in theexecute
method. In my case, I get my entity manager just at the start of theexecute
method like this and everything is working fine (tested with Symfony 2.1).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 thegetContainer
method tyring to do:Since
getApplication
is not an object yet, you get the a error saying or are calling a methodgetKernel
on a non-object.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.Update 2: In Symfony 4, auto-wiring can be used to reduce amount of code needed.
Create a
__constructor
with aEntityManagerInterface
variable. This variable will be accessible in the rest of your commands. This follows the auto-wiring Dependency Injection scheme.Credits to @profm2 for providing the comment and the code sample.
从 ContainerAwareCommand 而不是 Command 扩展命令类
并获取实体管理器,如下所示:
extends your command class from ContainerAwareCommand instead of Command
and get entity manager like this :
我知道 Matt 的回答解决了这个问题,但是如果您有多个实体管理器,您可以使用以下命令:
Make model.xml with:
然后将此文件加载到您的 DI 扩展中
然后您可以在任何地方使用它。
在控制台命令中执行:
并且不要忘记最后:
您现在可以在 services.yml 中将其用作另一个服务中的参数:
希望这对某人有帮助。
I know that Matt's answer solved the question, But if you've more than one entity manager, you can use this:
Make model.xml with:
Then load this file in your DI extension
Then you can use it anywhere.
In Console Command execute:
and don't forget at end to :
You can now use it as a argument in another service in services.yml:
Hope this help someone.