【发布时间】:2018-06-09 11:13:05
【问题描述】:
我想像这样注入抽象类:
services:
App\Infrastructure\Persistence\BaseDoctrineRepository:
arguments:
$eventStore: '@broadway.event_store'
$registry: '@doctrine'
$eventBus: '@broadway.event_handling.event_bus'
,但如果这样做,我会得到:
Cannot autowire service "App\Infrastructure\Persistence\User\DoctrineUserRepository": argument "$eventStore" of method "__construct()" references interface "Broadway\EventStore\EventStore" but no such service exists. You should maybe alias this interface to one of these existing services: "broadway.event_store.dbal", "broadway.event_store.in_memory".
所以我需要像这样为每个存储库复制代码,我想避免它。
services:
App\Infrastructure\Persistence\User\DoctrineUserRepository:
arguments:
$eventStore: '@broadway.event_store'
$registry: '@doctrine'
$eventBus: '@broadway.event_handling.event_bus'
抽象类:
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
abstract class BaseDoctrineRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, EventStore $eventStore, EventBus $eventBus)
{
$this->eventStore = $eventStore;
$this->eventBus = $eventBus;
parent::__construct($registry, static::REPOSITORY_CLASS);
}
从抽象类扩展的类(我想避免构造函数):
class DoctrineUserRepository extends BaseDoctrineRepository implements UserRepository
{
const REPOSITORY_CLASS = User::class;
public function __construct(ManagerRegistry $registry, EventStore $eventStore, EventBus $eventBus)
{
parent::__construct($registry, $eventStore, $eventBus);
}
【问题讨论】:
-
你使用的是哪个版本的 symfony?
-
Symfony 4,很抱歉忘记指定
标签: php symfony dependency-injection doctrine