简体   繁体   中英

$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. So below is my code.

CustomValidator.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.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:

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.

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. I was actually using this CustomValidator inside a CustomCommand . 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. 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

Firstly, thanks to @Trappar for answering my question. 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:

<?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 already extends Command itself and you will also get the container which will be provided by application kernel like below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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