5

I have a bunch of models setup in Doctrine, where some of the models are in different databases. Doctrine's schema generation tool seems to be generating inter-database foreign keys, but not cross-database foreign keys.

For example:

/** * @ORM\Entity * @ORM\Table(name="db1.Contact") **/ class Contact { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue **/ private $id; } /** * @ORM\Entity * @ORM\Table(name="db2.Subscription") **/ class Subscription { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue **/ private $id; /** * @ORM\ManyToOne(targetEntity="Contact") * @ORM\JoinColumn(name="contact_id", referencedColumnName="id") */ private $contact; } 

Critically, hydrating these entities works totally fine, but the schema tool simply doesn't generate the foreign keys.

Has anyone ran into this before? There is another SO post however it is unfortunately unanswered.

2
  • 2
    Doctrine seems not to support it. See previous topic : stackoverflow.com/questions/28234273/… Commented May 12, 2015 at 14:29
  • I managed to solve it. See below. Commented May 12, 2015 at 15:53

1 Answer 1

8

Doctrine does not support cross-database foreign keys, however it can be modified to do so. The library seems to take a "not everyone can support it, so no one should" approach. This instance works for MySQL.

When generating a schema, a pre-step is ran using the RemoveNamespacedAssets visitor. This removes all references to any classes outside of what you are generating.

In the acceptForeignKey function of that class, comment the following code:

// The table may already be deleted in a previous // RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that // point to nowhere. if ( ! $this->schema->hasTable($fkConstraint->getForeignTableName())) { $localTable->removeForeignKey($fkConstraint->getName()); return; } 

Running a schema creation or update will now create foreign keys as expected. It is possible this will have other unintended side effects but I haven't ran into any yet.

Sign up to request clarification or add additional context in comments.

3 Comments

All your databases will need to be on the same mysql server. Mysql won't query across multiple database servers. And of course you are hard coding the database name into your entity mappings. But if neither of these limitations impact your application then it should work just fine.
Yeah, that's my situation --- multiple databases but on the same server. Hard coding the database names into the mappings is extremely gross, I agree. I'm actually just going to move everything over to one database but this works as a quick fix!
After commenting the lines you said, the update tool gives me "There is no table with name 'db1.Contact' in the schema". I had to comment out the whole function content for it to work. I hope it won't break anything...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.