doctrine / data-fixtures

Doctrine2 ORM Data Fixtures Extensions
http://www.doctrine-project.org
MIT License
2.77k stars 224 forks source link

Set Container Is Not Working When Implements ContainerAwareInterface #268

Closed welenlin closed 7 years ago

welenlin commented 7 years ago

Hello, I already try to inject container based on the documentation here and still not working, is there something wrong with my code or the DI itself is not working ?

doctrine fixture version: "doctrine/doctrine-fixtures-bundle": "2.2.*", symfony version : 2.8.7

class LoadDataDefault extends AbstractFixture implements ContainerAwareInterface
{

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

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

}

Thanks :)

stof commented 7 years ago

well, what do you mean by "is not working" ? What is the error you get ?

welenlin commented 7 years ago

@stof when i try to var_dump("test");exit; in the setContainer , it doesnt trigger the var_dump so i'm assuming that it doesnt call the setContainer function method, and in the load() method i tried to use $this->container->get('session'); it triggers "fatal error: Call to a member function get() on null"

Ocramius commented 7 years ago

@welenlin that needs an example on how to reproduce the issue: the fixture loader on its own doesn't do the injection on its own, that's done by symfony-specific details while running the CLI command.

welenlin commented 7 years ago

@Ocramius did you mean that i should run doctrine:fixtures:load in order to inject the container ?

Ocramius commented 7 years ago

@welenlin yes: the functionality provided by the raw doctrine/data-fixtures has no container awareness.

welenlin commented 7 years ago

@Ocramius is there any other way than running the command ? since i'm using it in integration test case with the setup loadFixturesFromDirectory and i need to access the container there

Ocramius commented 7 years ago

As I already said, this package does know nothing about the container: it is a purely symfony-bundle-specific feature.

alcaeus commented 7 years ago

@welenlin the magic is done by the DataFixtures bridge within Symfony, namely Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader.

welenlin commented 7 years ago

@Ocramius @alcaeus

Okay I understand now, thanks for the help :)