如何在Doctrine中创建关系

时间:2011-06-12 15:40:57

标签: php symfony1 doctrine relational-database symfony-1.4

我和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 doctrine:build --all   --and负载

我有错误

  

SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败(jobeetjobeet_job,CONSTRAINT jobeet_job_categorya_id_jobeet_categorya_id FOREIGN KEY({{ 1}})参考categorya_idjobeet_categorya)ON DELETE CASCADE)

为什么?

1 个答案:

答案 0 :(得分:1)

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

如果这确实是你的问题,你可以在这里找到几个关于如何解决这个问题的线程...基本上,只需先构建(build --all),然后分批加载灯具,这样他们就会尊重外键关系。