如何在学说中建立关系


How to create a relation in Doctrine

我用Jobeet学习Symfony和Doctrine。我想在JobeetJob中添加关系。这是原件:http://www.symfony-project.org/jobeet/1_4/Doctrine/en/03

我确实喜欢这个:

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 }

当我这样做时:

php-symfony原则:build-all--并加载

我的有错误

SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(jobeet.jobeet_job,constraint jobeet_job_categorya_id_jobeet_categorya_id foreign key(categorya_id)REFERENCES jobeet_categoryaid)ON DELETE CASCADE)

为什么?

我认为问题是由该命令的--and-load部分引起的。当您以违反外键关系的顺序(即将子数据放在父数据之前)将固定装置加载到表中时,经常会出现这种错误。

如果这确实是你的问题,你可以在这里找到几个关于如何解决这个问题的线索。。。基本上,只需先构建(build --all),然后分批次加载fixture,以便它们尊重外键关系。