Doctrine does not create external database foreign keys

I have a set of model settings in Doctrine where some models are in different databases. Doctrine's schema generation tool appears to create foreign keys between databases, but not foreign 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, humidifying these objects works completely fine , but the schema tool just doesn't generate foreign keys.

Has anyone encountered this before? There is still a SO post , but unfortunately it doesn't answer.

+3


source to share


1 answer


Doctrine does not support cross-database foreign keys, however this can be changed. It seems that the library "not everyone can support it, so no one should" fit. This instance works for MySQL.

When creating a diagram, a preliminary step is performed using a visitor RemoveNamespacedAssets

. This removes all references to any classes outside of what you are generating.

In the function of acceptForeignKey

this class, comment out 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 may have other unintended side effects, but I haven't encountered them yet.

+5


source







All Articles