JMSDiExtraBundle icon indicating copy to clipboard operation
JMSDiExtraBundle copied to clipboard

@Inject dont works on Commands

Open rkmax opened this issue 11 years ago • 5 comments
trafficstars

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() {}
   }

rkmax avatar Jul 09 '14 04:07 rkmax

Yes, confirmed.

pestaa avatar Oct 02 '14 12:10 pestaa

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.

hoshsadiq avatar Oct 04 '14 22:10 hoshsadiq

@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

rkmax avatar Oct 05 '14 13:10 rkmax

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

ghost avatar Nov 12 '14 22:11 ghost

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

flip111 avatar Nov 08 '15 14:11 flip111