[英]Use entity manager in service
有没有办法访问服务中的实体管理器? 尽管我认为我必须使用依赖项注入,但在symfony文档中找不到任何内容。 我正在使用symfony 4。
只需将其注入构造函数即可:
use Doctrine\ORM\EntityManagerInterface
class YourService
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
// ...
}
由于采用了自动接线,因此不需要额外的配置。
这是注入了实体管理器的简单类的示例,您可以将其注册为服务:
namespace My\AppBundle;
use Doctrine\ORM\EntityManagerInterface;
class YourServiceName
{
/**
* @var EntityManagetInterface
*/
private $em;
public function __construct(EntityManagerInterface $em) : void
{
$this->em = $em;
}
}
并在services.yml
:
services:
your.service.name:
class: My\AppBundle\YourServiceName
arguments: [ @doctrine.orm.default_entity_manager]
是的你可以,
use Doctrine\Common\Persistence\ObjectManager;
public function __construct(ObjectManager $manager)
{
$this->manager = manager;
}
使用EntityManagerInterface到您的服务并检查自动装配,否则您需要注入它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.