Fabian
Fabian

Reputation: 905

Binding Entity Manager To Entity in Symfony2?

I've got 2 Bundles in my Symfony2 Framework: A "MainBundle" with a User Entity, and a "TicketBundle" with all my Stuff for the Issue Tracking System. In this Case only the Entities "Pool" and "Mappingpooluser" are important. They both use different entity mangers, because i have to use 2 different databases. (see my config.yml below)

doctrine:
  dbal:
    default_connection: default
    connections:
        default:
               [driver, host etc.]
               dbname:   m_symfony
        ticket:
               [driver, host etc.]
               dbname:   m_ticketbundle


orm:
  default_entity_manager: default
  entity_managers:
    default:
      connection: default
      mappings:
        XXXMainBundle: ~
    ticket:
      connection: ticket
      mappings:
        XXXTicketBundle: ~
        XXXMainBundle: ~

Now I have an Entity (Mappingpooluser) which needs one Entity of the MainBundle (User) and one Entity (Pool) of the TicketBundle (and some unimportant stuff):

/**
 * XXX\TicketBundle\Entity\Mappingpooluser
 *
 * @ORM\Table(name="MappingPoolUser")
 * @ORM\Entity(repositoryClass="XXX\TicketBundle\Repository\MappingPoolUserRepository")
*/
class Mappingpooluser
{

/**
 * @var integer $poolid
 *
 * @ORM\OneToOne(targetEntity="Pool")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 */
private $pool;

/**
 * @var integer $userid
 *
 * @ORM\OneToOne(targetEntity="XXX\MainBundle\Entity\User")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 */
private $user;

[getter/setter and this stuff]

Till this point everything works fine :) I can fetch the Mappingpooluser Entity in the Controller via

 $em = $this->getDoctrine()->getEntityManager("ticket");
 $entities = $em->getRepository('XXXTicketBundle:Mappingpooluser')->findAll();

If I call $entities[0]->getPool()->getId() I will get the correct ID of the Pool (as mentioned: Pool is in the same Bundle like Mappingpooluser), but if I call $entities[0]->getUser()->getId() I will get an Error:

 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'm_ticketbundle.User' doesn't exist 

Thats quit correct, because the User table is in the other DB (m_symfony).

Long Story, short Question: How do I get Symfony2 to use the ticket entity-manger for the Pool and the default entity-manger for the User??

Upvotes: 0

Views: 1674

Answers (1)

Aldo Stracquadanio
Aldo Stracquadanio

Reputation: 6237

The actual problem with your scenario is that you need to map the same entity (pool) to two different entity managers (default and ticket). What I would recommend in this scenario is creating a hierarchy in your entity layer for this entity:

  • base pool, contains all common fields
  • pool "default", contains specialized fields for default em
  • pool "ticket", contains specialized fields for ticket em

Then you can map them in different entity managers. If you use two different namespaces for default and ticket entities you can specify the folder from which mappings should be loaded in your em configuration like this:

mappings:   
  MyBundle:
    type: annotation
    dir: Path/To/Entities

The path is relative to the Bundles's root directory. So for example you could have Entity/Default and Entity/Ticket namespaces and map them independently while having common fields in an unmapped class in Entity namespace.

Upvotes: 3

Related Questions