使用 Doctrine 1.2 保存父/子标题/行
我的项目中有一个简单的父子/标题行模型。问题是它创建了一个循环关系或其他东西,并且不让我在没有孩子的情况下保存父母!检测关系已关闭!
detect_relations: false
...
BillHeader:
package: Billing
tableName: Bill_Headers
actAs:
SoftDelete: ~
columns:
id:
type: integer(8)
primary: true
notnull: true
autoincrement: true
....
BillLine:
package: Billing
tableName: Bill_Lines
actAs:
SoftDelete: ~
columns:
id:
type: integer(8)
primary: true
notnull: true
autoincrement: true
bill_header_id:
type: integer(8)
notnull: true
relations:
Bill_Header:
class: BillHeader
local: bill_header_id
foreign: id
foreignAlias: Bill_Lines
type: one
当我首先保存父级时:
$billHeader->save();
给出错误:SQLSTATE[HY000]:一般错误:1452 无法添加或更新子级行:外键约束失败 (sokidb
.bill_headers
, CONSTRAINTBill_Headers_id_Bill_Lines_bill_header_id
FOREIGN KEY (id
) REFERENCESbill_lines
(bill_header_id
))当我这样做时
$billHeader->Bill_Lines[] = $billLine;
给出错误:BillLine 不支持添加如果没有父行,该行将无法保存,因此我什至无法执行
$billHeader->link('Bill_Lines', $billLines);
$billHeader->Bill_Lines = $billLines ;
给出错误在设置一对多引用时无法调用 Doctrine_Core::set(),第二个参数应该是 Doctrine_Collection 的实例。如果我删除关系,请执行
$billHeader->save();
,然后$billHeader->id
返回空。所以这也不起作用!
我想知道是否有第六种方法可以做到这一点??? :(
我厌倦了思考这个问题,似乎没有解决方案。差不多 3 天了,现在有了线索!它让我有自杀倾向!为什么会出现这种行为?如果表是 MyIsam 而不是 InnoDB,会有帮助吗?
任何非常感谢对此的帮助! 提前致谢。
I have a simple parent-child/header-line model in my project. The problem is that its creating a cyclic relation or something, and not letting me save the parent without the child! Detect relations is switched off!
detect_relations: false
...
BillHeader:
package: Billing
tableName: Bill_Headers
actAs:
SoftDelete: ~
columns:
id:
type: integer(8)
primary: true
notnull: true
autoincrement: true
....
BillLine:
package: Billing
tableName: Bill_Lines
actAs:
SoftDelete: ~
columns:
id:
type: integer(8)
primary: true
notnull: true
autoincrement: true
bill_header_id:
type: integer(8)
notnull: true
relations:
Bill_Header:
class: BillHeader
local: bill_header_id
foreign: id
foreignAlias: Bill_Lines
type: one
when I save the parent first:
$billHeader->save();
gives error: SQLSTATE[HY000]: General error: 1452 Cannot add or update a child row: a foreign key constraint fails (sokidb
.bill_headers
, CONSTRAINTBill_Headers_id_Bill_Lines_bill_header_id
FOREIGN KEY (id
) REFERENCESbill_lines
(bill_header_id
))when I do
$billHeader->Bill_Lines[] = $billLine;
gives the error: Add is not supported for BillLineThe line won't save without the parent, so I can't even do
$billHeader->link('Bill_Lines', $billLines);
$billHeader->Bill_Lines = $billLines;
gives the error Couldn't call Doctrine_Core::set(), second argument should be an instance of Doctrine_Collection when setting one-to-many references.If I drop the relationship, do a
$billHeader->save();
, then$billHeader->id
returns empty. So thats not working either!
I wonder if there is a 6th way of doing it??? :(
I'm tired of thinking on this problem, there seems to be no solution. Almost 3 days on this and now clue! its getting me suicidal! why this behaviour? Will it help if the table is MyIsam instead of InnoDB?
Any help on this is much appreciated!
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的架构有问题。约束:
不应该存在。 BillHeader 的 id 不应该是 BillLine 的外键。相反,应该存在从 BillLine 到 BillHeader 的约束,例如:
您是否在 BillHeader 上定义了可能添加该约束的其他关系?无论如何,这里的关键是使用您的模式,直到约束朝着正确的方向发展。
Something is wrong with your schema. The constraint:
Should not exist. The id of BillHeader should not be a foreign key of BillLine. Instead, there should be a constraint from BillLine to BillHeader, something like:
Do you have additional relations defined on BillHeader that may be adding that constraint? Regardless, the key here is to play with your schema until the constraint is going in the right direction.
我经过深思熟虑后解决了这个问题。声明的属性之一被标记为 PRIMARY。不知道为什么会出现这样的结果,但这就是问题所在!
I solved it after much introspection. One of the attributes declared was marked PRIMARY. Don't know why this result, but this was the problem!