问题描述
我试图在控制器外面getDoctrine()。
我创建了这个服务:
I'm trying to getDoctrine() outside of the controller.I've created this service:
config / services.yml
config/services.yml
services:
update_command:
class: project\projBundle\Command\Update
arguments: ['@doctrine.orm.entity_manager']
和我的app / config / config.yml
and in my app/config/config.yml
imports:
- { resource: "@projectprojBundle/Resources/config/services.yml" }
所以我想使用的类:
so and the class that I want to use:
namespace project\projBundle\Command;
use Doctrine\ORM\EntityManager;
class Update {
protected $em;
public function __construct(EntityManager $em) {
$this->em = $em;
}
但每次我想这样做:(这样做吧?
but every time I want to do this: (I'm doing this right?)
$up = new Update();
我收到此错误:
Catchable Fatal Error: Argument 1 passed to ...\Update::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in .../Update.php line 7
推荐答案
strong>
Simple solution
如果您正在实现Symfony命令(可以在cron选项卡中执行),则可以从命令访问服务容器。
If you're implementing a Symfony command (that can be executed in a cron tab), you can access the service container from the command.
<?php
namespace MyProject\MyBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class UpdateCommand extends ContainerAwareCommand
{
protected $em;
protected function configure()
{
$this->setName('myproject:mybundle:update') ;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->em = $this->getContainer()->get('doctrine.orm.entity_manager');
}
}
这样,您就可以从命令中获取实体管理器并且不需要将此类声明为服务。因此,您可以删除在 services.yml
文件中添加的配置。
That way, you get the entity manager from a command and don't need to declare this class as a service. You can therefore remove the configuration you added in the services.yml
file.
其他解决方案更清洁)
此解决方案可以更好地分离问题,因此可以轻松地在Symfony应用程序的其他部分进行单元测试和重用(不仅仅是一个命令)
This solution allows better separation of concerns and can therefore be easily unit tested and reused in other parts of your Symfony application (not only as a command).
将update命令的所有逻辑部分移动到您将声明为服务的专用类:
Move all the logic part of your "update" command to a dedicated class that you will declare as a service:
<?php
namespace MyProject\MyBundle\Service;
use Doctrine\ORM\EntityManager;
class MyUpdater
{
protected $em;
public function __construct($em)
{
$this->em = $em;
}
public function runUpdate()
{
// All your logic code here
}
}
在您的 services.yml
文件中声明为服务:
Declare it as a service in your services.yml
file:
services:
myproject.mybundle.myupdater:
class: MyProject\MyBundle\Service\MyUpdater
arguments: ['@doctrine.orm.entity_manager']
只需拨打您的服务您的命令:
Simply call your service from your command :
<?php
namespace MyProject\MyBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class UpdateCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('myproject:mybundle:update') ;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$myUpdater = $this->getContainer()->get('myproject.mybundle.myupdater');
$myUpdater->runUpdate();
}
}
这篇关于在模型/控制器之外的Symfony2 getdoctrine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!