schmittjoh / JMSDiExtraBundle

Provides Advanced Dependency Injection Features for Symfony2
http://jmsyst.com/bundles/JMSDiExtraBundle
330 stars 130 forks source link

@Inject dont works on Commands #172

Open rkmax opened 10 years ago

rkmax commented 10 years ago

I have a Command (non-controller class) i'm traying to inject a doctrine mongo odm service

i've tried booth methods (independently) and when i execute the command i get the error Call to a member function xxxxx() on a non-object

Only works the Constructor/Setter Injection

/**
 * Class LoadPlainFileCommand
 * @package MyBundle\Command
 * @DI\Service(id="load_files")
 */
class LoadPlainFileCommand extends Command {

    /** 
     * @DI\Inject("doctrine_mongodb.odm.default_document_manager")
     */
    private $dm;

   //...
    /**
     * @return DocumentManager
     * @DI\LookupMethod("doctrine_mongodb.odm.default_document_manager")
     */
    protected function getDM() {}
   }
pestaa commented 10 years ago

Yes, confirmed.

hoshsadiq commented 10 years ago

I may be misunderstanding your issue, but have you tried setting the variable to public? If I understand correctly, this bundle has it's own code for injecting into controller classes, but other services are done through Symfony, and Symfony does not allow private variables to have a service unless you inject it through a method. You can do it on public variables. In controllers, because this bundle handles it, it takes appropriate action and you can inject on private variables.

rkmax commented 10 years ago

@hoshsadiq the idea is to try all 3 injection methods available, property injection and injection in through the method and the command just does not work

ghost commented 10 years ago

@InjectParams on the constructor also doesn't work for Symfony2 Command subclasses. Related?

flip111 commented 9 years ago

I have the same issue, doesn't matter if the property is private, protected or public