简体   繁体   English

在自定义的Symfony命令中使用Doctrine?

[英]Using Doctrine in a custom Symfony command?

I created a custom Symfony command to manage entities in my DB but I don't know how to use doctrine in it (eg import it like in a controller). 我创建了一个自定义的Symfony命令来管理数据库中的实体,但是我不知道如何在其中使用主义(例如,像在控制器中一样导入它)。

Thanks! 谢谢!

使用ContainerAwareCommand很好。

$this->getContainer()->get('doctrine');

Using the container directly is not a good practice. 直接使用容器不是一个好习惯。 Sometimes you will need to modify class code and when that time comes you will need to review the whole code to find any dependencies. 有时您将需要修改类代码,而到那时,您将需要查看整个代码以找到任何依赖项。

Register the command as a serivce and pass serivces that are actually will be used in the logic. 将命令注册为服务,并传递实际将在逻辑中使用的服务。

Check How to Define Commands as Services 检查如何将命令定义为服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM