0

I have 2 entity managers, each for one of my databases DATABASE_URL and DATABASE_URL_SAGE:

# config/packages/doctrine.yaml
doctrine:
  dbal:
    default_connection: site
    connections:
      site:
        url: '%env(resolve:DATABASE_URL)%'
        driver: 'pdo_sqlsrv'
        # ...
      sage:
        url: '%env(resolve:DATABASE_URL_SAGE)%'
        driver: 'pdo_sqlsrv'
        # ...

  orm:
    auto_generate_proxy_classes: true
    default_entity_manager: site
    entity_managers:
      site:
        connection: site
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        mappings:
          Site:
            is_bundle: false
            type: attribute
            dir: '%kernel.project_dir%/src/Entity/Site'
            prefix: 'App\Entity\Site'
            alias: Site
      sage:
        connection: sage
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        mappings:
          Sage:
            is_bundle: false
            type: attribute
            dir: '%kernel.project_dir%/src/Entity/Sage'
            prefix: 'App\Entity\Sage'
            alias: Sage

I would like to create an entity which:

  • has a ManyToOne relation with another entity managed by the same entity manager (site)
  • has another ManyToOne relation with another entity managed by the sage entity manager

Something like this:

// src/Entity/Site/CategoryFArticle.php
namespace App\Entity\Site;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class CategoryFArticle
{
    #[ORM\ManyToOne(targetEntity: 'App\Entity\Site\Category')]
    private $category;

    #[ORM\ManyToOne(targetEntity: 'App\Entity\Sage\FArticle')]
    private $fArticle;
}

The problem is when I do that I got the error:

Mapping
-------

 [FAIL] The entity-class App\Entity\Site\CategoryFArticle mapping is invalid:
 * The target entity 'App\Entity\Sage\FArticle' specified on App\Entity\Site\CategoryFArticle#fArticle is unknown or not an entity.


Database
--------


In MappingException.php line 23:
                                                                                                         
  The class 'App\Entity\Sage\FArticle' was not found in the chain configured namespaces App\Entity\Site  
                                                                                                         

Questions

  • Is doctrine able to manage this kind of mapping (with entities managed by differents entity managers) ?
  • If so, how can I configure my current code to work ?

Thank you

Lenny4
  • 1,215
  • 1
  • 14
  • 33
  • Having relations between entities in different entity managers will not work. Why do you want to have different entity managers? – goulashsoup Apr 19 '22 at 20:29
  • Because I have 2 databases – Lenny4 Apr 19 '22 at 20:33
  • 1
    Just like you have no foreing key between different DBs (potentially on different servers or RDBMS) there cannot be a relation at the object level, as objects are managed by the entity manager. Having two of them means no relations between them. At most you can save the primary key (as a simple type) and make the "join" manually by issuing a second query at the other DB. – Alejandro Apr 19 '22 at 20:59
  • 1
    Of course, my bad sry. In this case unfortunately you have to save the `FArticle`s as ids only and query them, when you need them. Doctrine handles associations per entity manager (`UnitOfWork`), each can only hold one connection, therefore associations between different databases are not possible. Also read https://stackoverflow.com/questions/42417452/doctrine-relations-between-two-entities-in-two-databases – goulashsoup Apr 19 '22 at 21:02
  • Thank you @Alejandro and goulashsoup, now I know. I will store the id of my entity and then load it with another query probably in a postload event. https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/events.html#postload – Lenny4 Apr 20 '22 at 12:02
  • 1
    @Lenny4 Using the post load event is a good thing for filling that gap, however be aware that it can cause N+1 problems in the second DB, as the event fires once per loaded object and not per whole query result. If you're experiencing performance problems you may need to load them manually all in one go. – Alejandro Apr 20 '22 at 12:05

0 Answers0