简体   繁体   English

$this->getContainer() 在 Symfony 3.x 中为 Command 返回 null

[英]$this->getContainer() is returning null in Symfony 3.x for Command

I am working on a project.我正在做一个项目。 I have some custom validator which is basically checking some data integrity in my database before saving any new record.我有一些自定义验证器,它基本上在保存任何新记录之前检查我的数据库中的某些数据完整性。 So for that check, I need to access the Doctrine entity manager.因此,对于该检查,我需要访问 Doctrine 实体管理器。 So below is my code.所以下面是我的代码。

CustomValidator.php自定义验证器.php

<?php
namespace Custom\Validator;

use ....


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

This AbstractModel class is basically implementing ContainerAwareInterface like below:这个 AbstractModel 类基本上实现了 ContainerAwareInterface,如下所示:

AbstractModel.php抽象模型.php

<?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);
    }
}

Inside my service.yml file, i defined AbstractModel dependencies like below:在我的service.yml文件中,我定义了 AbstractModel 依赖项,如下所示:

services:

    custom.abstract_model:
        class: Custom\Model\AbstractModel
        abstract: true
        public: true
        calls:
            - [setContainer, ["@service_container"]]

Now, when I am trying to run the validator, I am getting below error:现在,当我尝试运行验证器时,出现以下错误:

Call to a member function get() on null

Which indicates this line inside AbstractModel class.这表示 AbstractModel 类中的这一行。

return $this->getContainer()->get('doctrine')->getManager();

Is there anything wrong with my implementation?我的实现有什么问题吗? I tried to search but I failed to find any useful solution.我试图搜索,但没有找到任何有用的解决方案。

Update更新

I believe I didn't explain 10% of the whole scenario and that 10% was the vital part.我相信我没有解释整个场景的 10%,而那 10% 是至关重要的部分。 I was actually using this CustomValidator inside a CustomCommand .我实际上是在CustomCommand使用这个CustomValidator That was the main problem, I will explain below if anyone faces a similar problem like me later.这是主要问题,如果有人以后遇到类似的问题,我将在下面解释。

CustomValidator needs to have a parent service specified in the services.yaml. CustomValidator 需要在 services.yaml 中指定父服务。 I believe the following would do the trick:我相信以下方法可以解决问题:

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

See: https://symfony.com/doc/3.4/service_container/parent_services.html请参阅: https : //symfony.com/doc/3.4/service_container/parent_services.html

Firstly, thanks to @Trappar for answering my question.首先,感谢@Trappar 回答我的问题。 His answer is perfect for the scenario I described earlier.他的回答非常适合我之前描述的场景。 But I did not put whole details and the missing part was the most important part.但是我没有把全部细节都写出来,缺少的部分是最重要的部分。

So, as I updated my question, I will explain the scenario now.所以,当我更新我的问题时,我现在将解释这个场景。

I had a CustomCommand which looked like this:我有一个看起来像这样的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);
        }
    }
}

The thing is, in such was I was not getting the container I wanted.问题是,在这种情况下,我没有得到我想要的容器。 So, I went through some extensive research and figured out that for such cases, you have to extend ContainerAwareCommand and not Command .因此,我进行了一些广泛的研究,并发现对于这种情况,您必须扩展ContainerAwareCommand而不是Command ContainerAwareCommand already extends Command itself and you will also get the container which will be provided by application kernel like below. ContainerAwareCommand已经扩展了Command本身,您还将获得由应用程序内核提供的容器,如下所示。

$this->container = $application->getKernel()->getContainer();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM