2011-06-12 40 views
0

我通過Jobeet學習了Symfony和Doctrine。我想在JobeetJob中添加關係。 這是原文: http://www.symfony-project.org/jobeet/1_4/Doctrine/en/03如何在Doctrine中創建關係

我這樣做:

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-load

我有錯誤

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)

爲什麼呢?

+0

帕特里克,你應該把你的標題作爲一個問題來制定,因爲Jobeet是symfony/doctrine,並且把它放在那裏。這有助於其他人發現他們可能回答的問題。只是一個建議:'如何在Doctrine中建立關係?' – hakre 2011-06-12 15:54:11

+0

當你遇到錯誤時,你會怎麼做? – hakre 2011-06-12 15:54:43

+0

我這樣做:php symfony doctrine:build --all - and-load – 2011-06-12 16:11:54

回答

1

我在想這個問題是由該命令的--and-load部分引起的。當您按照違反其外鍵關係的順序(即將子數據放入父數據之前)將夾具加載到表中時,通常會出現此錯誤。

如果這確實是你的問題,你可以在這裏找到幾個線程關於如何解決這個問題......基本上,只需建立第一個(build --all),然後在不同的批次中加載燈具,以便他們尊重外鍵關係。