【问题标题】:Zend/Doctrine can't perform ORM operations, table already existsZend/Doctrine 不能执行 ORM 操作,表已经存在
【发布时间】:2015-03-02 21:16:03
【问题描述】:

我从另一个团队继承了一个项目,但似乎无法对数据库做任何事情。我是 zend 和教义的总 n00b,但 ORM 工具似乎很简单;但是,在尝试使用它时,我从orm:schema-tool:droporm:schema-tool:createorm:schema-tool:update 等收到同样的错误。

  [Doctrine\DBAL\Schema\SchemaException]                            
  The table with name 'mydb.alerts_residents' already exists.

我的数据库已创建,但没有表。我读过的其他帖子让我得出结论,这条消息是基于我的实体对象定义和注释。

您可能会怀疑,alerts_residents 是一个连接表,它以多对多关系将Alert 实体与Resident 实体连接起来。这些是唯一引用此表的实体,而且它们似乎正确地这样做了。

class Resident
{

    /**
     * @var ArrayCollection $zone
     *
     * @ORM\ManyToMany(targetEntity="Alert")
     * @ORM\JoinTable(
     *      name="alerts_residents",
     *      joinColumns={@ORM\JoinColumn(name="resident_id", referencedColumnName="id", onDelete="CASCADE")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="alert_id", referencedColumnName="id", onDelete="CASCADE")}
     * )
     */
    protected $alerts;


class Alert
{
    /**
     * @var ArrayCollection $zone
     *
     * @ORM\ManyToMany(targetEntity="Resident")
     * @ORM\JoinTable(
     *      name="alerts_residents",
     *      joinColumns={@ORM\JoinColumn(name="alert_id", referencedColumnName="id", onDelete="CASCADE")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="resident_id", referencedColumnName="id", onDelete="CASCADE")}
     * )
     */
    protected $residents;

没有@ORM\Table(name="alerts_residents") 的实体。为什么会出现此错误?

【问题讨论】:

    标签: php mysql orm doctrine-orm zend-framework2


    【解决方案1】:

    您已经冗余定义了多对多。 M2M 关联有点奇怪,因为连接表没有自己的实体。因此,一方(或多或少)被任意选择为拥有方,这就是获取连接表详细信息的实体。您的示例在两者上都有 @ORM\JoinTable 注释。

    http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#owning-and-inverse-side-on-a-manytomany-association

    【讨论】:

    • 谢谢,这似乎解决了问题。现在可以删除、创建和更新。
    猜你喜欢
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2023-03-10
    • 2015-01-28
    • 2017-12-04
    相关资源
    最近更新 更多