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

时间:2019-03-15 12:00:25

标签: php symfony oop dependency-injection symfony-3.4

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

我认为这很简单,但是它标记了__construct部分:

  

缺少父构造函数调用

我在BaseService中创建了该逻辑,并且可以正常工作

class BaseService
{
 /** @var ContainerInterface  */
    var $container;
    var $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);
}

和我的其他服务:

class DataService extends AbstractAdmin
{

public function __construct(BaseService $baseService)
{
    $this->baseService = $baseService;
}

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

2 个答案:

答案 0 :(得分:-1)

您忘记在DataService上添加AbstractAdmin的父构造函数。

class DataService extends AbstractAdmin
{

    public function __construct(BaseService $baseService)
    {
        parent::__construct(AbstractAdmin dependencies goes here);
        $this->baseService = $baseService;
    }

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

我不知道哪些依赖项需要您的AbstractAdmin

答案 1 :(得分:-1)

我找到了答案。

我是这样做的:

public $baseService;

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

由于Abstract Admin具有其构造函数。