Doctrine 2协会没有外键约束 [英] Doctrine 2 association without foreign key constraints
问题描述
我正在将一个旧的PHP应用程序转换为Symfony 2.应用程序数据目前不是很一致,所以我想避免创建外键约束。我在产品实体类中有以下注释:
class Product {
//一些定义
/ **
* @ ORM\ManyToOne(targetEntity =Manufacturer)
* @ ORM\JoinColumn(name =manufacturer_id,referencedColumnName = id)
* /
private $ Manufacturer;
}
当我执行 app / console doctrine:schema:更新
,我得到SQL命令
ALTER TABLE products ADD CONSTRAINT FK_F6FA18741C3BF575
FOREIGN KEY (manufacturer_id)REFERENCES manufacturer(id);
如何避免这种情况?
我最近必须经历相同的过程,幸运的是有一个简单的解决方案,只需添加 nullable = true
到列的注释。
只要现有数据有效,我就会工作,在我的情况下,我必须将0更改为NULL,并将不再存在的键更改为NULL。 / p>
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
/**
* @ORM\ManyToOne(targetEntity="Manufacturer")
* @ORM\JoinColumn(name="manufacturer_id", referencedColumnName="id" )
*/
private $Manufacturer;
}
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?
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.
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屋!