How to inject non-default entity managers?

前端 未结 4 765
太阳男子
太阳男子 2020-12-28 19:01

In Symfony2 you can work with multiple entity managers and use something like the code below:

$em = $this->get(\'doctrine\')->getManager();
$em = $this         


        
相关标签:
4条回答
  • 2020-12-28 19:51

    You should define your custom entity manager as a service:

    services:  
        name_of_your_custom_manager:
            class: %doctrine.orm.entity_manager.class%
            factory_service:  doctrine
            factory_method:   getEntityManager
            arguments: ["name_of_your_custom_manager"]
    

    Then, you can inject it in the same way as you do with every service:

    @name_of_your_custom_manager
    

    Edit:

    Pay attention that factory method may differ between symfony's version (it could be getEntityManager or getManager)

    0 讨论(0)
  • 2020-12-28 19:53

    Hello first of all create your manager, in my example I create the manager for my Item class that is in a CoreBundle:

    <?php
    // src/Sybio/Bundle/CoreBundle/Manager/ItemManager.php:
    
    namespace Sybio\Bundle\CoreBundle\Manager;
    
    use Sybio\Bundle\CoreBundle\Entity\Item;
    
    class ItemManager
    {
        /**
         * @var \Doctrine\ORM\EntityManager $em entity manager
         */
        protected $em;
    
        /**
         * @var \Doctrine\ORM\EntityRepository $em repository
         */
        protected $repository;
    
        /**
         * @var string $entityName
         */
        protected $entityName;
    
        /**
         * Constructor
         *
         * @param EntityManager $em
         * @param string $entityName
         * 
         * @return void
         */
        public function __construct(EntityManager $em, $entityName)
        {
            $this->em = $em;
            $this->repository = $em->getRepository($entityName);
            $this->entityName = $entityName;
        }
    
        /**
         * Save a entity object
         *
         * @param Object $entity
         * 
         * @return Object Entity
         */
        public function save($entity)
        {
            $this->persistAndFlush($entity);
    
            return $entity;
        }
    
        /**
         * Remove a entity object
         *
         * @param Object $entity
         * 
         * @return Object Entity
         */
        public function remove($entity)
        {
            return $this->removeAndFlush($entity);
        }
    
        /**
         * Persist object
         *
         * @param mixed $entity
         * 
         * @return void
         */
        protected function persistAndFlush($entity)
        {
            $this->em->persist($entity);
            $this->em->flush();
        }
    
        /**
         * Remove object
         *
         * @param mixed $entity entity to remove
         * 
         * @return void
         */
        protected function removeAndFlush($entity)
        {
            $this->em->remove($entity);
            $this->em->flush();
        }
    
        /**
         * Returns entity repository object
         * 
         * @return EntityRepository
         */
        public function getRepository()
        {
            return $this->repository;
        }
    
        /**
         * Create a new object
         * 
         * @return mixed
         */
        public function createNewObject()
        {
            return new Item();
        }
    
        // Create your own methods to manage the object
    
    }
    

    If the manager structure is shared between multiple manager, you can create a BaseManager extended by all other managers !

    Then register it in the services.yml (or xml) file of your bundle:

    # src/Sybio/Bundle/CoreBundle/Resources/config/services.yml or xml !:
    
    parameters:
    
        # Managers _________________
    
        sybio.item_manager.entity: SybioCoreBundle:Item
        sybio.item_manager.class: Sybio\Bundle\CoreBundle\Manager\ItemManager
    
    services:
    
        # Managers _________________
    
        sybio.item_manager:
            class:        %sybio.item_manager.class%
            arguments:    [@doctrine.orm.entity_manager, %sybio.item_manager.entity%]
    

    That's it, you can now use it:

    // Controller:
    
    $im =  $this->get('sybio.item_manager');
    
    $item = $im->createNewObject();
    $im->save($item);
    

    You can then improve your manager, here I give an array of config parameters to my manager:

    # src/Sybio/Bundle/CoreBundle/Resources/config/services.yml or xml !:
    
    sybio.item_manager:
            class:        %sybio.item_manager.class%
            arguments:    [@doctrine.orm.entity_manager, %sybio.item_manager.entity%, {'item_removed_state': %item_removed_state%, 'item_unpublished_state': %item_unpublished_state%, 'item_published_state': %item_published_state%}]
    
    
    // src/Sybio/Bundle/CoreBundle/Manager/ItemManager.php:
    
    public function __construct(EntityManager $em, $entityName, $params = array()) {
        // ...
        $this->params = $params;
    }
    

    If you create a BaseManager, you can also create a usefull generic method to initialize an object:

    // src/Sybio/Bundle/CoreBundle/Manager/BaseManager.php:
    
    /**
     * Create a new object
     * 
     * @return mixed
     */
    public function createNewObject()
    {
        $entityName = explode(":", $this->entityName);
        $entityName = "Sybio\Bundle\CoreBundle\Entity\\".$entityName[1];
    
        return new $entityName;
    }
    
    0 讨论(0)
  • 2020-12-28 19:54

    For those who are using Symfony 3+, use the console : php bin/console debug:container

    Then you should see many lines starting with : 'doctrine.orm.MY_CUSTOM_ENTITY_MANAGER_xxxxxxxxxx'

    So if you want the entity manager corresponding to your custom entity manager, find the line : 'doctrine.orm.MY_CUSTOM_ENTITY_MANAGER_entity_manager'

    You can insert it in your service arguments.

    Hope it helps.

    0 讨论(0)
  • 2020-12-28 20:03

    If your entity managers config name is non_default then you can reference it as @doctrine.orm.non_default_entity_manager

    0 讨论(0)
提交回复
热议问题