简体   繁体   English

如何在 Doctrine 中创建关系

[英]How to create a relation in Doctrine

i learn Symfony and Doctrine with Jobeet.我用 Jobeet 学习 Symfony 和 Doctrine。 I would like add relation in JobeetJob.我想在 JobeetJob 中添加关系。 this is original: http://www.symfony-project.org/jobeet/1_4/Doctrine/en/03这是原创的: http://www.symfony-project.org/jobeet/1_4/Doctrine/en/03

I do like this:我喜欢这样:

JobeetCategory:
  actAs: { Timestampable: ~ }
  columns:
    name: { type: string(255), notnull: true, unique: true }

JobeetCategorya:
  actAs: { Timestampable: ~ }
  columns:
    name: { type: string(255), notnull: true, unique: true }

JobeetJob:
  actAs: { Timestampable: ~ }
  columns:
    category_id:  { type: integer, notnull: true }
    categorya_id:  { type: integer, notnull: true }
    type:         { type: string(255) }
    (...)
    expires_at:   { type: timestamp, notnull: true }
  relations:
    JobeetCategory: { onDelete: CASCADE, local: category_id, foreign: id, foreignAlias: JobeetJobs }
    JobeetCategorya: { onDelete: CASCADE, local: categorya_id, foreign: id, foreignAlias: JobeetJobsa }

when i do:当我做:

php symfony doctrine:build --all --and-load php symfony doctrine:build --all --and-load

i have error我有错误

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ( jobeet . jobeet_job , CONSTRAINT jobeet_job_categorya_id_jobeet_categorya_id FOREIGN KEY ( categorya_id ) REFERENCES jobeet_categorya ( id ) ON DELETE CASCADE) SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败( jobeet ,CONSTRAINT jobeet_job FOREIGN KEY jobeet_job_categorya_id_jobeet_categorya_id categorya_id ) REFERENCES jobeet_categorya ( id ) ON DELETE CASCADE)

why?为什么?

I'm thinking the problem is caused by the --and-load part of that command.我认为问题是由该命令的--and-load部分引起的。 That error comes up often when you're loading fixtures into tables in an order that would violate their foreign key relationships (ie putting child data in before parent data).当您以违反其外键关系的顺序将固定装置加载到表中时(即,将子数据放在父数据之前)时,经常会出现该错误。

If this is indeed your problem, you can find several threads here on how to work around this issue... basically, by just building first ( build --all ), and then loading fixtures in separate batches so they respect the foreign key relationships.如果这确实是你的问题,你可以在这里找到几个关于如何解决这个问题的线程......基本上,只需先构建( build --all ),然后分批加载固定装置,以便它们尊重外键关系.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM