2010-12-22 60 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

相关问题