Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php $this->;getContainer()在Symfony 3.x中为命令返回null_Php_Symfony 3.4_Symfony3.x - Fatal编程技术网

Php $this->;getContainer()在Symfony 3.x中为命令返回null

Php $this->;getContainer()在Symfony 3.x中为命令返回null,php,symfony-3.4,symfony3.x,Php,Symfony 3.4,Symfony3.x,我正在做一个项目。我有一些自定义验证器,它基本上是在保存任何新记录之前检查数据库中的一些数据完整性。因此,为了进行检查,我需要访问条令实体管理器。下面是我的代码 CustomValidator.php <?php namespace Custom\Validator; use .... /** * Class CustomValidator * @package Custom\Validator */ class CustomValidator extends Abstract

我正在做一个项目。我有一些自定义验证器,它基本上是在保存任何新记录之前检查数据库中的一些数据完整性。因此,为了进行检查,我需要访问条令实体管理器。下面是我的代码

CustomValidator.php

<?php
namespace Custom\Validator;

use ....


/**
 * Class CustomValidator
 * @package Custom\Validator
 */
class CustomValidator extends AbstractModel
{
    public function validate()
    {
        //my validation rules
    }
}
<?php

namespace Custom\Model;

use ....


abstract class AbstractModel implements ContainerAwareInterface
{

     /**
     * @var ContainerInterface
     */
    protected $container;

    public function setContainer(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * @return ContainerInterface
     */
    protected function getContainer()
    {
        return $this->container;
    }
    /**
     * @return ObjectManager
     */
    public function getObjectManager()
    {
        return $this->getContainer()->get('doctrine')->getManager();
    }

    /**
     * @param $entityClass
     * @return ObjectRepository
     */
    public function getRepository($entityClass)
    {
        return $this->getObjectManager()->getRepository($entityClass);
    }
}
现在,当我尝试运行验证器时,出现以下错误:

Call to a member function get() on null
这表示AbstractModel类中的这一行

return $this->getContainer()->get('doctrine')->getManager();
我的实现有什么问题吗?我试图搜索,但没有找到任何有用的解决办法

更新


我相信我没有解释整个场景的10%,而这10%是至关重要的部分。我实际上是在
CustomCommand
中使用这个
CustomValidator
。这是主要的问题,如果以后有人遇到类似的问题,我将在下面解释。

CustomValidator需要在services.yaml中指定父服务。我相信以下几点可以奏效:

services:
    Custom\Validator\CustomValidator:
        parent: '@custom.abstract_model'

请参阅:

首先,感谢@Trappar回答我的问题。他的答案非常适合我前面描述的情景。但我没有把全部细节都说出来,而遗漏的部分才是最重要的部分

所以,当我更新我的问题时,我现在将解释这个场景

我有一个
CustomCommand
,看起来像这样:

<?php

namespace Custom\Command;

class CustomCommand extends Command
{

    protected function configure()
    {
         //my configuration
    }


    protected function execute(InputInterface $input, OutputInterface $output)
    {
            //my custom code

            $oValidator = new CustomValidator();
            $oValidator->validate($aParams);
        }
    }
}

虽然这确实是解决眼前问题的正确解决方案,但真正的问题是永远不要注入整个容器。正确的解决方案是使用构造函数注入将所需的服务注入到验证器中,或者使用
ServiceSubscriberInterface
达到同样的效果。哦,当然,如果他们使用自动连线,那么他们可以使用
\u instanceof
,而无需声明抽象服务或标记父服务。我只是在回答问题。好的,我回答了。谢谢。:)
<?php

namespace Custom\Command;

class CustomCommand extends Command
{

    protected function configure()
    {
         //my configuration
    }


    protected function execute(InputInterface $input, OutputInterface $output)
    {
            //my custom code

            $oValidator = new CustomValidator();
            $oValidator->validate($aParams);
        }
    }
}
$this->container = $application->getKernel()->getContainer();