2010-12-22 81 views
0

我發現在這個問題上的一些有用的信息,但不能完全包住我的頭周圍的解決方案。所以,我希望有人能向我解釋一個有益的解決方案,而不是一個插件/黑客/解決方法,我拼命的做了「正確」的方式:Symfony的1.4 embedRelation完整性約束違規

所以這是我的問題:

模式:

detect_relations: true  
Student: 
    tableName: student 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    name: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '' 
     notnull: true 
     autoincrement: false 
    parents_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '' 
     notnull: true 
     autoincrement: false 
    relations: 
    Parents: 
     refClass: StudentParentLink 
     local: student_id 
     foreign: parents_id 
     onDelete: CASCADE 
Parents: 
    tableName: parents 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    name: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '' 
     notnull: true 
     autoincrement: false 
    email_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '0' 
     notnull: true 
     autoincrement: false 
    relations: 
     Student: 
     refClass: StudetParentLink 
     local: parents_id 
     forign: student_id 
     onDelete: CASCADE 
Email: 
    tableName: email 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    email: 
     type: string(255) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '' 
     notnull: true 
     autoincrement: false 
StudentParentLink: 
    tableName: student_parent_link 
    columns: 
    id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: true 
     autoincrement: true 
    student_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '0' 
     notnull: true 
     autoincrement: false 
    parents_id: 
     type: integer(4) 
     fixed: false 
     unsigned: false 
     primary: false 
     default: '0' 
     notnull: true 
     autoincrement: false 

因此,在英語,我有一個學生誰有父(S)和父(S)有一個電子郵件地址,夠簡單。

所以在我的學生表格,我有以下:

//studentForm.class.php 
public function configure() 
{ 
    if($this->getObject()->isNew() || count($this->getObject()->Parents) == 0) 
    { 
     $this->getObject()->Parents[] = new Parents(); 
    } 

    $parentsSubForm = new sfForm(); 
    $i = 1; 
    foreach($this->getObject()->Parents as $parents) 
    { 
     $parentsForm = new ParentsForm($parents); 
     $parentSubForm->embedForm("Parent $i",$parentsForm); 
     $i++; 
    } 
    $this->embedForm('Parents',$parentSubForm) 
} 

這看起來如預期工程將學生記錄,但是在更新我的錯誤:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2' for key 1 

我米不知道發生了什麼,只是它看起來像是做一個插入不更新爲父,是否按設計?我只需要能夠添加/通過學生表

編輯父母的電子郵件(和所有其他數據點在這個例子中沒有列出爲簡單起見)

一如往常的任何指示或意見都非常歡迎!我只是得到Symfony的吊and,像這樣的細微差別很好學習,所以我可以前進!

〜馬洛Zjoia

UPDATE

所以我拼命在這裏,完全糊塗了,我用盡了一切我能想到的,發現和是的,我能得到的錯誤走開但究竟是在上述模式曖昧是EMAIL是多到一個這樣的東西,如高級形式描述不工作,你需要它涉及不同的,所以我有以下代碼:

if($this->getObject()->isNew() || count($this->getObject()->Email) < 1) 
{ 
    $email = new Email($this->getObject()->Email); 
    $emailForm = new EmailForm($email); 
    $this->embedForm('Parents Email', $emailForm); 
    $useFields[] = 'Parents Email'; 
}else{ 
    $this->embedRelation('Email'); 
    $this->widgetSchema['Email']->setLabel('Parents Email'); 
    $useFields[] = 'Email'; 
} 

該作品正好 如果我在父窗體,但是當我在學生表(其中嵌入了父母形成)這不是有關的電子郵件回父,它正確地創建了電子郵件在email table,但不插入在parent table

的EMAIL_ID

我很生氣,我只是不明白,請幫忙!

IDIOT

ANSWER

原來我有這樣的被鏈接,我認爲一個模型重建會刪除原來沒有這麼多,得到表一些老型號的移除,並且所有的瘋狂怪異消失了,事情完美無缺!

總是愚蠢的東西被遺漏

回答

0

你的問題是很常見的,並在symfony的網站描述。 Here就是你需要的例子。

也可以是有用此documentation。「尤其是第十一章 - 學說整合」。

Registers Evgeny

+0

檢查更新信息...仍然需要一些幫助! – Zjoia 2011-01-07 09:42:13

相關問題