Php Symfony 3不会将EntityManager注入服务
我使用symfony3.1 我正在尝试将EntityManager注入到我的服务类中。我所做的一切都和文档中的一样,但仍然不断出现异常Php Symfony 3不会将EntityManager注入服务,php,doctrine-orm,symfony,Php,Doctrine Orm,Symfony,我使用symfony3.1 我正在尝试将EntityManager注入到我的服务类中。我所做的一切都和文档中的一样,但仍然不断出现异常 [Symfony\Component\Debug\Exception\FatalThrowableError] Type error: Argumen
[Symfony\Component\Debug\Exception\FatalThrowableError]
Type error: Argument 1 passed to AppBundle\Writers\TeamsWriter::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /ho
me/admin_u/Documents/test_project/src/AppBundle/Command/ParseMatchesCommand.php on line 54
为什么它不在服务类中注入原则
服务
private $entity_manager;
/**
* TeamsWriter constructor.
* @param EntityManager $entity_manager
*/
public function __construct(EntityManager $entity_manager)
{
$this->entity_manager = $entity_manager;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$parser = new TeamsParser();
$data = $parser->execute();
$writer = new TeamsWriter();
$writer->store($data);
}
use AppBundle\Parsers\TeamsParser;
use AppBundle\Writers\TeamsWriter;
final class YourCommand extends Command
{
// ... use constructor injection to get your dependencies
public function __construct(TeamsWriter $teamsWriter, TeamsParser $teamsParser)
{
$this->teamsWriter = $teamsWriter;
$this->teamsParser = $teamsParser;
parent::__construct(); // this is need if this is console command, to setup name and description
// kinda hidden dependency but it is the way it works now
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$data = $this->teamsParser->execute();
$this->teamsWriter->store($data);
}
}
Services.yml
services:
teams_writer:
class: AppBundle\Writers\TeamsWriter
arguments: ["@doctrine.orm.entity_manager"]
services:
_defaults:
autowire: true # all services here will be autowired = no need for manual service naming in constructor
autoconfigure: true # this will add tags to all common services (commands, event subscribers, form types...)
AppBundle\Writers\TeamsWriter: ~ # short notation for a service
AppBundle\Parsers\TeamsParser: ~
AppBundle\Command\YourCommand: ~
服务使用情况
private $entity_manager;
/**
* TeamsWriter constructor.
* @param EntityManager $entity_manager
*/
public function __construct(EntityManager $entity_manager)
{
$this->entity_manager = $entity_manager;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$parser = new TeamsParser();
$data = $parser->execute();
$writer = new TeamsWriter();
$writer->store($data);
}
use AppBundle\Parsers\TeamsParser;
use AppBundle\Writers\TeamsWriter;
final class YourCommand extends Command
{
// ... use constructor injection to get your dependencies
public function __construct(TeamsWriter $teamsWriter, TeamsParser $teamsParser)
{
$this->teamsWriter = $teamsWriter;
$this->teamsParser = $teamsParser;
parent::__construct(); // this is need if this is console command, to setup name and description
// kinda hidden dependency but it is the way it works now
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$data = $this->teamsParser->execute();
$this->teamsWriter->store($data);
}
}
你没有使用服务,只使用课堂。要在命令中使用服务,请通过
containerawarecomand
对其进行扩展,您可以从中调用您的服务:
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
class MyCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$writer = $this->getContainer()->get('teams_writer');
正如@malcolm所述,您还需要将命令注册为服务 由于Symfony 3.3几天后就要放松了,我将使用。我将只重写您已经发布的代码: Services.yml
services:
teams_writer:
class: AppBundle\Writers\TeamsWriter
arguments: ["@doctrine.orm.entity_manager"]
services:
_defaults:
autowire: true # all services here will be autowired = no need for manual service naming in constructor
autoconfigure: true # this will add tags to all common services (commands, event subscribers, form types...)
AppBundle\Writers\TeamsWriter: ~ # short notation for a service
AppBundle\Parsers\TeamsParser: ~
AppBundle\Command\YourCommand: ~
服务使用情况
private $entity_manager;
/**
* TeamsWriter constructor.
* @param EntityManager $entity_manager
*/
public function __construct(EntityManager $entity_manager)
{
$this->entity_manager = $entity_manager;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$parser = new TeamsParser();
$data = $parser->execute();
$writer = new TeamsWriter();
$writer->store($data);
}
use AppBundle\Parsers\TeamsParser;
use AppBundle\Writers\TeamsWriter;
final class YourCommand extends Command
{
// ... use constructor injection to get your dependencies
public function __construct(TeamsWriter $teamsWriter, TeamsParser $teamsParser)
{
$this->teamsWriter = $teamsWriter;
$this->teamsParser = $teamsParser;
parent::__construct(); // this is need if this is console command, to setup name and description
// kinda hidden dependency but it is the way it works now
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$data = $this->teamsParser->execute();
$this->teamsWriter->store($data);
}
}
Hm您需要使用
条令\ORM\EntityManager
在您的团队writer中,或将解析器和编写器直接注入命令,避免注入容器。