0

I'm trying to access to the service api.service.feedback in fixture class below but for some reason container returns error below on if statement in load() method:

ERROR: PHP Fatal error: Call to a member function has() on a non-object

Note: If anyone wants to know, the service api.service.feedback itself is accessible in all the controllers and has no problem with it.

<?php

namespace .........;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

class LoadOrderFixtures extends AbstractFixture implements FixtureInterface, ContainerAwareInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

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

    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        if ($this->container->has('api.service.feedback')) {
            exit('EXISTS');
        }
     }

     ......
}
BentCoder
  • 12,257
  • 22
  • 93
  • 165

2 Answers2

0

You are just implementing the ContainerAwareInterface. If you want the container, you have to inject it:

services:
    bundle.service_name: 
        class: ...
        calls:
            - [ setContainer, [ @service_container ] ]
devilcius
  • 1,764
  • 14
  • 18
  • You don't need that! [Doc](http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#using-the-container-in-the-fixtures) – BentCoder Dec 05 '14 at 11:18
  • @inanzzz you're right, according to documentation the container should be injected with `setContainer` just by implenting the `ContainerAwareInterface`. But apparently it's not happening. There's also a note in that doc: *If you are too lazy to implement the needed method setContainer(), you can then extend your class with ContainerAware.* Have your tried extending it? – devilcius Dec 05 '14 at 11:27
  • @inanzzz this is weird, have you checked this question:? http://stackoverflow.com/questions/16789730/container-dependecy-injection-not-working-in-fixtures-after-upgrading-symfony-2 – devilcius Dec 05 '14 at 11:41
  • Just did it but still no luck. – BentCoder Dec 05 '14 at 11:47
0

You can extend directly ContainerAware:

class LoadArticlesData extends ContainerAware implements FixtureInterface, OrderedFixtureInterface {

    public function load(ObjectManager $manager)
    {
        $dummy = $this->container->getParameter('dynamic.dummy');
        ...   
    }
}
Cristian Bujoreanu
  • 1,147
  • 10
  • 21