0

I have this situation

abstract class Importer {

    const NW = 1;

    public static function getInstance($type)
    {
        switch($type)
        {
            case(self::NW):
            return new NWImporter();
            break;
        }
    }

    protected function saveObject(myObject $myObject)
    {
        //here I need to use doctrine to save on mongodb
    }

    abstract function import($nid);
}

and

class NWImporter extends Importer
{
    public function import($nid)
    {
        //do some staff, create myObject and call the parent method to save it
        parent::saveObject($myObject);
    }
}

and I want to use them like this

 $importer = Importer::getInstance(Importer::NW);
 $importer->import($nid);

my question is: how to inject doctrine to be used in saveObject method?

thanks

3
  • Declare your class as service passing argument @doctrine.orm.entity_manager Commented Aug 4, 2016 at 14:58
  • can you provide an example? I tried already with services Commented Aug 4, 2016 at 15:04
  • 1
    see below @elkorchianas anwser :) Commented Aug 4, 2016 at 15:23

1 Answer 1

1

You need to configure your importer as a symfony service :

services:
    test.common.exporter:
        # put the name space of your class
        class:  Test\CommonBundle\NWImporter 
        arguments: [ "@doctrine" ]

then in NWImporter define a constructor with a parameter that will have the doctrine instance

public function __construct($doctrine)
  {
    $this->doctrine= $doctrine;
  }

with this solution you can avoid using a factory method as symfony does it for you but if you wanna to keep it, When you call $importer = Importer::getInstance(Importer::NW); from your controller you can inject the doctrine argument in your factory method :

abstract class Importer {

    const NW = 1;

    public static function getInstance($type, $doctrine)
    {
        switch($type)
        {
            case(self::NW):
            return new NWImporter($doctrine);
            break;
        }
    }

    protected function saveObject(myObject $myObject)
    {
        //here I need to use doctrine to save on mongodb
    }

    abstract function import($nid);
}

then in your controller you should to do something like that :

 $doctrine = $this->container->get('doctrine');
 $importer = Importer::getInstance(Importer::NW, $doctrine);
 $importer->import($nid);
Sign up to request clarification or add additional context in comments.

5 Comments

I tried already but I got an error because Importer has "return new NWImporter();" expecting I pass a parameter...
You are calling NWImporter::getInstance() from where is it from your controller ?
yes I want to call it from a controller, and, I can't inject to Importer because NWImporter extends importer, and once again the construct expects a parameter...
they both work but if I want to use the factory pattern (and I want to use it because I need several different importers) I must pass doctrine to the factory. Now I don't know if this is a good practice but it works so thanks you.
you welcome :), I think it's still a good practice because the main purpose of the factory pattern is to instantiate and configure dependencies of a service which is the case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.