symfony Shopware 6:如何使用自动装配注入实体存储库

sgtfey8w  于 2023-10-23  发布在  PWA
关注(0)|答案(2)|浏览(222)

使用Shopware 6通过services.xml注入实体存储库的“经典”方法如下

# your service/controller
private EntityRepository $productRepository;
...
public function __construct(
   EntityRepository $productRepository
) {
   $this->$productRepository = $productRepository;
}
...
<!-- your services.xml -->
<service id="...">
  <argument type="service" id="product.repository"/>
</service>

现在,如果我想使用Symfony的自动装配-什么会相当于上述符号?
(In换句话说,我这里的观点是如何告诉Symfony我想要连接product.repository服务,而不仅仅是通用的repository?)

kqhtkvqz

kqhtkvqz1#

快速解释它是如何在内部工作的,以便您更好地理解。
Shopware首先找到定义,然后在CompilerPass过程中动态创建存储库。
https://github.com/shopware/platform/blob/v6.5.5.1/src/Core/Framework/DependencyInjection/CompilerPass/EntityCompilerPass.php#L35
在容器(aliasDefinitions属性)中,它的结果如下:

Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $aclRoleRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $aclUserRoleRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appActionButtonRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appActionButtonTranslationRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appCmsBlockRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appCmsBlockTranslationRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appFlowActionRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appFlowActionTranslationRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appFlowEventRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appPaymentMethodRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appScriptConditionRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appScriptConditionTranslationReposito = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appTemplateRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $appTranslationRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $customFieldRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $customFieldSetRelationRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $customFieldSetRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $languageRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $logEntryRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $pluginRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $pluginTranslationRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $scheduledTaskRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $scriptRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $seoUrlRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $seoUrlTemplateRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $versionCommitDataRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $versionCommitRepository = {Symfony\Component\DependencyInjection\Alias} 
Shopware\Core\Framework\DataAbstractionLayer\EntityRepository $versionRepository = {Symfony\Component\DependencyInjection\Alias}

[.....]
[.....]
[.....]
[.....]

所以,换句话说,通常你有这些定义:

MySampleDefinition.php
HelloWorldDefinition.php
MyCustomEntityDefinition.php

你将能够在PHP文件上自动访问这样的存储库:

use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;

public function __construct(
    private EntityRepository $mySampleRepository,
    private EntityRepository $helloWorldRepository,
    private EntityRepository $myCustomEntityRepository,
) {
    
}

不必担心手工定义存储库.

jtoj6r0c

jtoj6r0c2#

我发现的解决方案实际上相当简单和直接:-)使用Autowire参数属性指定构造函数,如下所示:

private readonly EntityRepository $productRepository;
    ...
    public function __construct(
        #[Autowire(service: 'product.repository')] EntityRepository $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

如果你遵守Shopware的命名约定,你甚至可以省略Autowire参数,在上面的例子中,变量名$productRepository将被正确解析为'product.repository'服务。(礼貌@Alex的评论见下文)

相关问题