没有外键约束的 Doctrine 2 关联 [英] Doctrine 2 association without foreign key constraints

查看:20
本文介绍了没有外键约束的 Doctrine 2 关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将遗留 PHP 应用程序转换为 Symfony 2.目前应用程序数据不是很一致,所以我想避免创建外键约束.我的产品"实体类中有以下注释:

I'm in the process of converting a legacy PHP application to Symfony 2. The application data is not very consistent at the moment, so I would like to avoid creating foreign key constraints. I have the following annotation in my "Product" entity class:

class Product {

  // some definitions

  /**
   * @ORMManyToOne(targetEntity="Manufacturer")
   * @ORMJoinColumn(name="manufacturer_id", referencedColumnName="id" )
   */
  private $Manufacturer;
}

当我执行app/console police:schema:update时,我得到了SQL命令

When I do app/console doctrine:schema:update, I get the SQL command

 ALTER TABLE products ADD CONSTRAINT FK_F6FA18741C3BF575 
   FOREIGN KEY (manufacturer_id) REFERENCES manufacturer(id);

我怎样才能避免这种情况?

How can I avoid this?

推荐答案

我最近也经历了同样的过程,幸好有一个简单的解决方案,只需将 nullable=true 添加到列的注释中.

I had to go through the same process recently and fortunately there is an easy solution, just add nullable=true to the column's annotation.

只要现有数据有效,这就会起作用,在我的情况下,我必须将 0 更改为 NULL,并将不再存在的键更改为 NULL.

This will work as long as the existing data is valid, in my case I had to change 0's to NULL's and change keys that didn't exist anymore to NULL.

这篇关于没有外键约束的 Doctrine 2 关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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