使用 Doctrine 1.2 保存父/子标题/行

发布于 2024-09-10 19:22:19 字数 1759 浏览 2 评论 0原文

我的项目中有一个简单的父子/标题行模型。问题是它创建了一个循环关系或其他东西,并且不让我在没有孩子的情况下保存父母!检测关系已关闭!

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
  1. 当我首先保存父级时:$billHeader->save(); 给出错误:SQLSTATE[HY000]:一般错误:1452 无法添加或更新子级行:外键约束失败 (sokidb.bill_headers, CONSTRAINT Bill_Headers_id_Bill_Lines_bill_header_id FOREIGN KEY (id) REFERENCES bill_lines (bill_header_id))

  2. 当我这样做时 $billHeader->Bill_Lines[] = $billLine; 给出错误:BillLine 不支持添加

  3. 如果没有父行,该行将无法保存,因此我什至无法执行 $billHeader->link('Bill_Lines', $billLines);

  4. $billHeader->Bill_Lines = $billLines ; 给出错误在设置一对多引用时无法调用 Doctrine_Core::set(),第二个参数应该是 Doctrine_Collection 的实例。

  5. 如果我删除关系,请执行 $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
  1. 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, CONSTRAINT Bill_Headers_id_Bill_Lines_bill_header_id FOREIGN KEY (id) REFERENCES bill_lines (bill_header_id))

  2. when I do $billHeader->Bill_Lines[] = $billLine; gives the error: Add is not supported for BillLine

  3. The line won't save without the parent, so I can't even do $billHeader->link('Bill_Lines', $billLines);

  4. $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.

  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2024-09-17 19:22:20

您的架构有问题。约束:

CONSTRAINT Bill_Headers_id_Bill_Lines_bill_header_id  FOREIGN KEY (id) REFERENCES bill_lines (bill_header_id))

不应该存在。 BillHeader 的 id 不应该是 BillLine 的外键。相反,应该存在从 BillLine 到 BillHeader 的约束,例如:

CONSTRAINT `Bill_Lines_header_id_Bill_Header_id` FOREIGN KEY (`bill_header_id`) REFERENCES `Bill_Header` (`id`)

您是否在 BillHeader 上定义了可能添加该约束的其他关系?无论如何,这里的关键是使用您的模式,直到约束朝着正确的方向发展。

Something is wrong with your schema. The constraint:

CONSTRAINT Bill_Headers_id_Bill_Lines_bill_header_id  FOREIGN KEY (id) REFERENCES bill_lines (bill_header_id))

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:

CONSTRAINT `Bill_Lines_header_id_Bill_Header_id` FOREIGN KEY (`bill_header_id`) REFERENCES `Bill_Header` (`id`)

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.

温柔嚣张 2024-09-17 19:22:20

我经过深思熟虑后解决了这个问题。声明的属性之一被标记为 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文