尝试升级现有的Symfony 3.3.2项目以使用自动装配 .
按照此处的说明操作:https://symfony.com/doc/current/service_container/3.3-di-changes.html#content_wrapper .
对我不起作用的是能够将目录中的所有服务设置为公共 .
Important: 我的services.yml文件位于src / SiteBundle / Resources / config中,而不是app / config .
# src/SiteBundle/Resources/config/services.yml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
SiteBundle\Service:
resource: '../../Service'
public: true
CustomerOrderMasterRepository:
class: Doctrine\ORM\EntityRepository
factory: ["@doctrine.orm.entity_manager", get_repository]
arguments:
- SiteBundle\Entity\CustomerMaster
SiteBundle\Service\CustomersService:
arguments:
- '@doctrine.orm.entity_manager'
- '@CustomerMasterRepository'
如果我然后执行控制台调试:container | grep -i服务,它只显示输出头和Symfony服务容器 . src / SiteBundle / Service目录中的所有类都没有被选中 .
Symfony Container公共服务服务ID类名称service_container Symfony \ Component \ DependencyInjection \ ContainerInterface
如果我添加--show-private,那么它们会被显示(以及许多其他内容) .
Symfony Container公共和私有服务服务ID类名称1_bcf140bb848ef41617942628c8525b4872574e826d00fee6aaabbf2ede89fbb8 Symfony \ Component \ DependencyInjection \ ServiceLocator Psr \ Container \ ContainerInterface别名为“service_container”SiteBundle \ Service \ CustomerOrdersService SiteBundle \ Service \ CustomerOrdersService SiteBundle \ Service \ CustomersService SiteBundle \ Service \ CustomersService SiteBundle \ Service \ InventoryItemsService SiteBundle \ Service \ VendorOrsService SiteBundle \ Service \ VendorOrdersService SiteBundle \ Service \ VendorsService SiteBundle \ Service \ VendorsService SiteBundle \ Service \ WorkOrdersService SiteBundle \ Service \ WorkOrdersService Symfony \ Component \ DependencyInjection \ ContainerInterface“service_container”别名“argument_resolver.service Symfony \ Component \ HttpKernel \ Controller \ ArgumentResolver \ ServiceValueResolver routing.loader.service Symfony \ Component \ Routing \ Loader \ DependencyInjection \ ServiceRouterLoader secu rity.authentication.rememberme.services.abstract security.authentication.rememberme.services.persistent Symfony \ Component \ Security \ Http \ RememberMe \ PersistentTokenBasedRememberMeServices security.authentication.rememberme.services.simplehash Symfony \ Component \ Security \ Http \ RememberMe \ TokenBasedRememberMeServices service_container Symfony的\分量\ DependencyInjection \ ContainerInterface service_locator.1712c3a50d1ec2c742b2ead0f03bb76c的Symfony \分量\ DependencyInjection \服务定位service_locator.26ac001b3ede28481ac0de703666b4d7的Symfony \分量\ DependencyInjection \服务定位service_locator.39e66930232432ca5ba91e98fdd8a17b的Symfony \分量\ DependencyInjection \服务定位service_locator.6f24348b77840ec12a20c22a3f985cf7的Symfony \分量\ DependencyInjection \服务定位service_locator.8925f20c49cbd61fcb37adf8c595459e Symfony \ Component \ DependencyInjection \ ServiceLocator service_locator.b8d2046fb854cde05549fb309e1a80d2别名为“1_bcf140bb848ef41617942628c8525b4872574e82” 6d00fee6aaabbf2ede89fbb8“service_locator.ceb8bbb9f48e8bfd1c8ec2d209eabdca Symfony \ Component \ DependencyInjection \ ServiceLocator
如果我注释掉SiteBundle \ Service \ CustomerService定义,则debug:container命令会抛出此异常:
PHP致命错误:未捕获的Symfony \ Component \ DependencyInjection \ Exception \ AutowiringFailedException:无法自动装配服务“SiteBundle \ Entity \ CustomerMasterRepository”:方法“Doctrine \ ORM \ EntityRepository :: __ construct()”的参数“$ em”必须具有类型-hint或明确给出一个值 . 坦率地说,我无法做出头脑或尾巴 .
建议?谢谢 .
2 回答
这是主要问题:
当您使用EntityRepository作为服务时,会发生这种情况,但是继承自Doctrine BaseRepository . 这种组合永远不会发生 .
The repository should use composition, to get to Doctrine 喜欢这个:
There is a post with nice code examples if you want to know more: How to use Repository with Doctrine as Service in Symfony
要使自定义存储库工作,我必须覆盖
__construct
方法以提供EntityManager
和ClassMetadata
,如下所示:应用程序/配置/ services.yml
SRC /的appbundle / EntityRepository / ChannelRepository.php:
SRC /的appbundle /实体/ Channel.php