使用“必须实现接口 Doctrine\ORM\EntityManagerInterface 错误”将 EntityManagerInterface 注入我的服务
Inject EntityManagerInterface to my service with `must implement interface Doctrine\ORM\EntityManagerInterface error`
我在./src/Service创建了一个服务,我想在我的服务中使用Doctrine Entity Manager,所以我在__construct
方法中注入它:
命名空间App\Service;
use App\Entity\Category;
use Doctrine\ORM\EntityManagerInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class CommonPageGenerator
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var Environment
*/
private $templating;
public function __construct(EntityManagerInterface $em, Environment $templating)
{
$this->em = $em;
$this->templating = $templating;
}
public function page1($title){ return; }
}
然后我在控制器中注入这个服务:
/**
* @Route("/overseas", name="overseas")
* @param CommonPageGenerator $commonPageGenerator
*/
public function overseas(CommonPageGenerator $commonPageGenerator)
{
return $commonPageGenerator->page1('overseas');
}
但是我得到以下错误:
Argument 1 passed to App\Service\CommonPageGenerator::__construct() must implement interface Doctrine\ORM\EntityManagerInterface, string given, called in /Users/tangmonk/Documents/mygit/putixin.com/putixin_backend/var/cache/dev/ContainerB7I3rzx/getCommonPageGeneratorService.php on line 11
我的 services.yaml
文件:
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$em: 'doctrine.orm.default_entity_manager'
我正在使用 Symfony 4.3
您不需要绑定 $em
到 Doctrine 实体管理器。
如果删除该行并仅保留类型提示 (__construct(EntityManagerInterface $em, Environment $templating
) 应该就足够了。
因此您的 __construct()
方法如下:
// you can of course import the EngineInterface with a "use" statement.
public function __construct(
EntityManagerInterface $em,
Environment $templating)
{
$this->em = $em;
$this->templating = $templating;
}
如果您这样做并删除 绑定配置,自动依赖注入应该会自动运行。
(通常我会建议将Environment
替换为Symfony\Bundle\FrameworkBundle\Templating\EngineInterface
,以依赖框架提供的接口与模板组件集成。但是this component and its integration have been deprecated in 4.3 , 并将在 5.0 中删除;所以你直接依赖 Twig 就可以了。)
但是如果你出于某种原因想要保留绑定,你应该在服务名称前加上一个@
符号,这样Symfony就知道你正在尝试注入服务而不是字符串。像这样:
bind:
$em: '@doctrine.orm.default_entity_manager'
Docs.
我在./src/Service创建了一个服务,我想在我的服务中使用Doctrine Entity Manager,所以我在__construct
方法中注入它:
命名空间App\Service;
use App\Entity\Category;
use Doctrine\ORM\EntityManagerInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class CommonPageGenerator
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var Environment
*/
private $templating;
public function __construct(EntityManagerInterface $em, Environment $templating)
{
$this->em = $em;
$this->templating = $templating;
}
public function page1($title){ return; }
}
然后我在控制器中注入这个服务:
/**
* @Route("/overseas", name="overseas")
* @param CommonPageGenerator $commonPageGenerator
*/
public function overseas(CommonPageGenerator $commonPageGenerator)
{
return $commonPageGenerator->page1('overseas');
}
但是我得到以下错误:
Argument 1 passed to App\Service\CommonPageGenerator::__construct() must implement interface Doctrine\ORM\EntityManagerInterface, string given, called in /Users/tangmonk/Documents/mygit/putixin.com/putixin_backend/var/cache/dev/ContainerB7I3rzx/getCommonPageGeneratorService.php on line 11
我的 services.yaml
文件:
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$em: 'doctrine.orm.default_entity_manager'
我正在使用 Symfony 4.3
您不需要绑定 $em
到 Doctrine 实体管理器。
如果删除该行并仅保留类型提示 (__construct(EntityManagerInterface $em, Environment $templating
) 应该就足够了。
因此您的 __construct()
方法如下:
// you can of course import the EngineInterface with a "use" statement.
public function __construct(
EntityManagerInterface $em,
Environment $templating)
{
$this->em = $em;
$this->templating = $templating;
}
如果您这样做并删除 绑定配置,自动依赖注入应该会自动运行。
(通常我会建议将Environment
替换为Symfony\Bundle\FrameworkBundle\Templating\EngineInterface
,以依赖框架提供的接口与模板组件集成。但是this component and its integration have been deprecated in 4.3 , 并将在 5.0 中删除;所以你直接依赖 Twig 就可以了。)
但是如果你出于某种原因想要保留绑定,你应该在服务名称前加上一个@
符号,这样Symfony就知道你正在尝试注入服务而不是字符串。像这样:
bind:
$em: '@doctrine.orm.default_entity_manager'
Docs.