Doctrine 2 association without foreign key constraints(没有外键约束的 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 关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:没有外键约束的 Doctrine 2 关联


基础教程推荐
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01