Doctrine不产生跨数据库外键 [英] Doctrine doesn't generate cross-database foreign keys

查看:150
本文介绍了Doctrine不产生跨数据库外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Doctrine中设置了一堆模型,其中一些模型位于不同的数据库中。 Doctrine的模式生成工具似乎是生成数据库外键,而不是跨数据库外键。



例如:

  / ** 
* @ ORM\Entity
* @ ORM\Table(name =db1。联系人)
** /
class联系人{
/ **
* @ 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;
}

严格来说,保湿这些实体工作完全正常但是模式工具根本不会生成外键。



有没有人遇到过这个?还有另一个 SO post ,但它是不幸的是没有答复。

解决方案

Doctrine不支持跨数据库外键,但是可以修改它。图书馆似乎采取不是每个人都可以支持,所以没有人应该的方法。此实例适用于MySQL。



生成模式时,使用 RemoveNamespacedAssets 访问者运行前台步骤。这将删除您所生成的任何类的所有引用。



在该类的 acceptForeignKey 函数中,评论以下代码:

  //该表可能已被删除在以前的
// RemoveNamespacedAssets#acceptTable调用。删除外键
//指向无处。
if(!$ this-> schema-> hasTable($ fkConstraint-> getForeignTableName())){
$ localTable-> removeForeignKey($ fkConstraint-> getName());
return;
}

运行模式创建或更新现在将按预期创建外键。这可能会有其他意想不到的副作用,但我还没有遇到过。


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.

解决方案

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.

这篇关于Doctrine不产生跨数据库外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆