注入问题 - 缺少父构造函数调用

dev*_*ops 3 php oop dependency-injection symfony symfony-3.4

我试图将我的 BaseService 注入到另一个服务中,我需要调用我在 BaseService 中编写的存储库。

我认为这很简单,但它用以下标记 __construct 部分:

缺少父构造函数调用

我在 BaseService 中创建了该逻辑并且它有效

class BaseService
{
    /** @var ContainerInterface */
    public $container;
    public $em;

    public function __construct(ContainerInterface $container, EntityManagerInterface $em)
    {
        $this->container = $container;
        $this->em        = $em;
    }

    /**
     * @return \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
     */
    public function getMyDataRepository()
    {
        return $this->em->getRepository(MyData::class);
    }
}
Run Code Online (Sandbox Code Playgroud)

和我的其他服务:

class DataService extends AbstractAdmin
{
    public function __construct(BaseService $baseService)
    {
        $this->baseService = $baseService;
    }

    public function getTransactions(Card $card)
    {
        return $this->getMyDataRepository()
            ->createQueryBuilder('c')
            ->getQuery();
    }
}
Run Code Online (Sandbox Code Playgroud)

dev*_*ops 8

我找到了答案。

我是这样做的:

public $baseService;

public function __construct($code, $class, $baseControllerName, BaseService $baseService)
{
    parent::__construct($code, $class, $baseControllerName);
    $this->baseService = $baseService;
}
Run Code Online (Sandbox Code Playgroud)

由于 Abstract Admin 有其构造函数。