Forms Symfony 1.4嵌入关系完整性约束冲突

Forms Symfony 1.4嵌入关系完整性约束冲突,forms,symfony1,doctrine,Forms,Symfony1,Doctrine,我在这个问题上找到了一些有用的信息,但还不能完全理解解决方案。因此,我希望有人能向我解释一个有用的解决方案,而不是一个插件/黑客/变通方法,我正在努力以“正确”的方式: 我的问题是: 模式: detect_relations: true Student: tableName: student columns: id: type: integer(4) fixed: false unsigned: false primary:

我在这个问题上找到了一些有用的信息,但还不能完全理解解决方案。因此,我希望有人能向我解释一个有用的解决方案,而不是一个插件/黑客/变通方法,我正在努力以“正确”的方式:

我的问题是:

模式:

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
所以在英语中,我有一个学生,他有一个家长,家长有一个电子邮件地址,很简单

因此,在我的学生表格中,我有以下内容:

 //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的窍门,像这样的细微差别非常值得学习,这样我就可以继续前进了

~Mahalo Zjoia

更新

因此,我在这里感到绝望和完全困惑,我已经尝试了所有我能想到和找到的,是的,我能够让错误消失,但在上面的模式中不明确的是电子邮件是多对一的,所以按照预先表单描述的做事情不起作用,您需要将其与其他内容联系起来,因此我有以下代码:

 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';
 }
if($this->getObject()->isNew()| | count($this->getObject()->Email)<1)
{
$email=新电子邮件($this->getObject()->email);
$emailForm=新的emailForm($email);
$this->embedForm('Parents Email',$emailForm);
$useFields[]=“家长电子邮件”;
}否则{
$this->embedder关系(“电子邮件”);
$this->widgetSchema['Email']->setLabel('Parents Email');
$useFields[]=“电子邮件”;
}
如果我在家长表单中,这很好,但当我在学生表单(嵌入家长表单)中时,它不会将电子邮件关联回家长,它会在
电子邮件表中正确创建电子邮件,但不会在
家长表中插入电子邮件id

我快发疯了,我只是不明白,请帮帮我

白痴

回答

事实证明,我有一些旧模型,它们链接了我以为模型重建会删除的表,结果却没那么多,被删除了,所有疯狂的怪异都消失了,一切都很完美


总是错过一些愚蠢的事情

你的问题很常见,在symfony网站上有描述。 这是你需要的例子

这也可能有用。“特别是第11章——一体化原则”

问候
Evgeny

检查更新的信息…仍然需要一些帮助!