我有以下schema.yml
Proposition:
actAs: { Timestampable: ~ }
columns:
name: { type: string(255), notnull: true }
slug: { type: string(255), notnull: true, unique: true }
proposition_type_id: { type: integer, notnull: true }
icon: { type: string(255) }
overview: { type: string(4000) }
features: { type: string(4000) }
benefits: { type: string(4000) }
published: { type: boolean, notnull: true, default: 1 }
relations:
PropositionType: { onDelete: CASCADE, local: proposition_type_id, foreign: id }
Products:
class: Product
refClass: PropositionProduct
local: proposition_id
foreign: product_id
foreignAlias: PropositionProducts
PropositionType:
columns:
name: { type: string(255), notnull: true }
Review:
actAs: { Timestampable: ~ }
columns:
proposition_id: { type: integer, notnull: true }
review: { type: string(4000), notnull: true }
name: { type: string(255), notnull: true }
company: { type: string(255) }
published: { type: boolean, notnull: true, default: 1 }
relations:
Proposition: { onDelete: CASCADE, local: proposition_id, foreign: id }
PropositionProduct:
columns:
proposition_id: { type: integer, primary: true }
product_id: { type: integer, primary: true }
relations:
Proposition: { onDelete: CASCADE, local: proposition_id, foreign: id }
Product: { onDelete: CASCADE, local: product_id, foreign: id }
Product:
actAs: { Timestampable: ~ }
columns:
name: { type: string(255), notnull: true }
slug: { type: string(255), notnull: true, unique: true }
icon: { type: string(255) }
ataglance: { type: string(4000) }
idealfor: { type: string(4000) }
details: { type: string(4000) }
specsheet: { type: string(255) }
chart: { type: string(255) }
published: { type: boolean, notnull: true, default: 1 }
relations:
RelatedProducts:
class: Product
refClass: RelatedProduct
local: product_id
foreign: related_product_id
foreignAlias: RelatedProducts
RelatedProduct:
columns:
product_id: { type: integer, primary: true }
related_product_id: { type: integer, primary: true }
relations:
Product: { onDelete: CASCADE, local: product_id, foreign: id }
Product: { onDelete: CASCADE, local: related_product_id, foreign: id }
Segment:
actAs: { Timestampable: ~ }
columns:
name: { type: string(255), notnull: true }
slug: { type: string(255), notnull: true, unique: true }
published: { type: boolean, notnull: true, default: 1 }
relations:
Products:
class: Product
refClass: SegmentProduct
local: segment_id
foreign: product_id
foreignAlias: SegmentProducts
SegmentProduct:
columns:
segment_id: { type: integer, primary: true }
product_id: { type: integer, primary: true }
relations:
Segment: { onDelete: CASCADE, local: segment_id, foreign: id }
Product: { onDelete: CASCADE, local: product_id, foreign: id }
我跑了:
php symfony doctrine:build --all --and-load --no-confirmation
并且数据库已成功构建。
但为什么要创建proposition_segment
表?
CREATE TABLE `proposition_segment` (
`segment_id` bigint(20) NOT NULL DEFAULT '0',
`product_id` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`segment_id`,`product_id`),
KEY `proposition_segment_product_id_product_id` (`product_id`),
CONSTRAINT `proposition_segment_product_id_product_id` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE CASCADE,
CONSTRAINT `proposition_segment_segment_id_segment_id` FOREIGN KEY (`segment_id`) REFERENCES `segment` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
根据我的理解,我的方案应详细说明Segment
和Product
通过SegmentProduct
表格有多对多的关系。
同样,Proposition
和Product
通过PropositionProduct
表格有多对多的关系。
我无法理解为什么Doctrine正在创建一个proposition_segment
表。除此之外,数据库看起来是正确的 - 它按预期创建了表proposition_product
和segment_product
。
当我通过Symfony生成的管理员后端添加数据时,proposition_segment
表仍为空,这增加了我怀疑它是错误创建的。
答案 0 :(得分:0)
感谢greg0ire的链接,今天早上玩了几个小时之后,我决定创建一个新的空白Symfony项目并加载我的scheme.yml。构建它之后,我得到了我想要的数据库结构!所以事实证明该方案实际上是正确的,并且它必须是一个生成额外表的某个表单类?我会做更多的挖掘,试图找出发生了什么。我也改变了别名并根据你的建议创建了一个相等的嵌套关系 - 谢谢。
修改强>
宾果!我在lib / model /中发现了一些不需要的类,整理这个文件夹修复了生成的额外表。我虽然Doctrine只读取了config / doctrine / scheme.yml文件,但我想它也会读取你的模型? - 当然这样做是因为在所有示例中它都显示了创建类的两种不同方法。我还不确定这些文件何时生成,但从现在开始我会关注我的模型。