我如何在自定义存储库中注入类-Symfony 2.7 [英] How can i inject class inside custom repository - Symfony 2.7
问题描述
我有自定义存储库类,在其中必须注入可帮助我上传文件并在需要时擦除文件的类。我扩展了EntityRepository构造函数,但是我不知道如何在自定义存储库类中添加第三个参数。
I have custom repository class in which i must inject class that helps me to upload file, and erase file if needed. I expanded EntityRepository constructor, but i don't know how to add third argument inside custom repository class.
class NewRepository extends EntityRepository
{
protected $fileUploader;
public function __construct(EntityManager $em, Mapping\ClassMetadata $class,FileUploader $fileUploader)
{
parent::__construct($em, $class);
}
public function create($data, Item $item = null)
{
$em = $this->getEntityManager();
if(!$item) $item = new Item();
if(isset($data['file'])) {
$image = $this->fileUploader->setFile($data['file'])->uploadFile();
$data['filename'] = $image['filename'];
$data['image_url'] = $image['file_url'];
}
$item->setTitle($data['title']);
$item->setDescription($data['description']);
$em->persist($item);
$em->flush($item);
return $item;
}
}
我总是会错误地认为第三个参数传递给构造函数的值为null。
I always get error that third argument passed to constructor is null.
推荐答案
自版本2.4开始,Doctrine使用 RepositoryFactory
用于实例化存储库,并作为启动程序为您提供< a href = https://github.com/doctrine/doctrine2/blob/ee066cc5de7e8726d0be7a2d241235ba6c033cf8/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php rel = nofollow noreferrer> DefaultRepositoryFactory
。为了能够将更多的依赖项注入到您的存储库中,您需要对上述工厂进行自己的实现(为简便起见,我省略了所有 use
语句):
Since version 2.4 Doctrine uses a RepositoryFactory
for instantiating repositories and as a starter provides you with DefaultRepositoryFactory
. To be able to inject additional dependencies into your repositories you need to roll your own implementation of aforementioned factory (I'm omitting all the use
statements for brevity):
class YourRepositoryFactory implements RepositoryFactory
{
private $fileUploader;
public function __construct(FileUploader $fileUploader)
{
$this->fileUploader = $fileUploader;
}
public function getRepository(EntityManagerInterface $entityManager, $entityName)
{
$repositoryHash = $entityManager->getClassMetadata($entityName)->getName() . spl_object_hash($entityManager);
if (isset($this->repositoryList[$repositoryHash])) {
return $this->repositoryList[$repositoryHash];
}
return $this->repositoryList[$repositoryHash] = $this->createRepository($entityManager, $entityName);
}
private function createRepository(EntityManagerInterface $entityManager, $entityName)
{
/* @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
$metadata = $entityManager->getClassMetadata($entityName);
$repositoryClassName = $metadata->customRepositoryClassName
?: $entityManager->getConfiguration()->getDefaultRepositoryClassName();
switch ($repositoryClassName) {
case NewRepository::class:
return new $repositoryClassName($entityManager, $metadata, $this->fileUploader);
default:
return new $repositoryClassName($entityManager, $metadata);
}
}
}
将工厂注册为服务,您需要使用以下方式调整教义
的配置:
After registering your factory as a service in the way you prefer you need to adjust doctrine
configuration with:
doctrine:
orm:
repository_factory: name.of.your.factory.service
你很好!
这篇关于我如何在自定义存储库中注入类-Symfony 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!