0
votes

At beginning, I used repository.

But, after some code reviews on github, I'm interesting to use ObjectManager (to alleviate controllers, and also by curiosity ^^).

The problem is I didn't see some good tutorial about it. Even tutorials I saw was to initialize a service by an object manager but not to create one.

In the FriendsOfSymfony github, we could see an example for that but I don't really understand how to initialize the service. I have this error "Cannot instantiate interface Doctrine\Common\Persistence\ObjectManager" when I initialize my manager service like this:

<?xml version="1.0" ?>

http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
    <parameter key="md_mechanical.entity.enginemanager.class">MD\MechanicalBundle\Entity\EngineManager</parameter>
</parameters>

<services>
    <service id="md_mechanical.enginemanager.default" class="%md_mechanical.entity.enginemanager.class%">
        <argument type="service" id="md_mechanical.object_manager" />
        <argument>%md_engine.engine.class%</argument>
    </service>

    <!-- The factory is configured in the DI extension class to support more Symfony versions -->
    <service id="md_mechanical.object_manager" class="Doctrine\Common\Persistence\ObjectManager">
        <argument>%fos_user.model_manager_name%</argument>
    </service>
</services>

thanks in advance for your help

1

1 Answers

-1
votes

Cannot instantiate interface Doctrine\Common\Persistence\ObjectManager means you are trying to instantiate an interface, which is not possible.

You have to create an object which implements this interface, and define all functions

use Doctrine\Common\Persistence\ObjectManager as ObjectManager;

class MyObjectManager implements ObjectManager
{
    public function __construct(/* some params here */)
    {
        // Construct your manager here
    }

    public function find($className, $id)
    {
        // Do stuff
    }

    public function persist($object)
    {
        // Do stuff
    }

    public function remove($object)
    {
        // Do stuff
    }

    public function merge($object)
    {
        // Do stuff
    }

    public function clear($objectName = null)
    {
        // Do stuff
    }

    public function detach($object)
    {
        // Do stuff
    }

    public function refresh($object)
    {
        // Do stuff
    }

    public function flush()
    {
        // Do stuff
    }

    public function getRepository($className)
    {
        // Do stuff
    }

    public function getClassMetadata($className)
    {
        // Do stuff
    }

    public function getMetadataFactory()
    {
        // Do stuff
    }

    public function initializeObject($obj)
    {
        // Do stuff
    }

    public function contains($object)
    {
        // Do stuff
    }
}

Then declare it as a service

<services>
    <service id="myObjectManager" class="%myObjectManager.class%">
        <argument>...</argument>
    </service>

    # Use your brand new object manager
    <service id="md_mechanical.enginemanager.default" class="%md_mechanical.entity.enginemanager.class%">
        <argument type="service" id="myObjectManager" />
        <argument>%md_engine.engine.class%</argument>
    </service>
</services>

You should have a look at Doctrine\ORM\EntityManager and Doctrine\ORM\EntityManagerInterface, it may help you.