Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony 1.4 embedRelation Integrity constraint violation
    text
    copied!<p>I have found some useful info on this issue but can't quite wrap my head around the solutions. So I'm hoping someone can explain to me a helpful solution and not a plugin/hack/workaround I'm trying very hard to do the "Right" way:</p> <p>So here is what my issue:</p> <p>schema:</p> <pre><code>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 </code></pre> <p>So in english, I have a student who has a parent(s) and that parent(s) has an email address, simple enough.</p> <p>So in my Student Form I have the following:</p> <pre><code> //studentForm.class.php public function configure() { if($this-&gt;getObject()-&gt;isNew() || count($this-&gt;getObject()-&gt;Parents) == 0) { $this-&gt;getObject()-&gt;Parents[] = new Parents(); } $parentsSubForm = new sfForm(); $i = 1; foreach($this-&gt;getObject()-&gt;Parents as $parents) { $parentsForm = new ParentsForm($parents); $parentSubForm-&gt;embedForm("Parent $i",$parentsForm); $i++; } $this-&gt;embedForm('Parents',$parentSubForm) } </code></pre> <p>This looks as expected and works for adding a Student record, however on update I get the error:</p> <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2' for key 1 </code></pre> <p>I'm not sure what is happening only that it looks like it's doing an insert not update for the parent, is that as designed? I just need to be able to add/edit the parents email (and all other data points not listed in this example for simplicity) via the Student Form</p> <p>As always any direction or comments are very welcome! I am just getting the hang of Symfony and little nuances like this are great to learn so I can move forward!</p> <p>~ Mahalo Zjoia</p> <p><strong>UPDATE</strong></p> <p>So I am desperate here and totally confused, I've tried everything I can think of and find and yes I am able to get the error to go away but what is ambiguous in the above schema is EMAIL is Many-to-One so doing things as described in Advance Forms doesn't work you need to relate it different so I have the following Code:</p> <pre><code> if($this-&gt;getObject()-&gt;isNew() || count($this-&gt;getObject()-&gt;Email) &lt; 1) { $email = new Email($this-&gt;getObject()-&gt;Email); $emailForm = new EmailForm($email); $this-&gt;embedForm('Parents Email', $emailForm); $useFields[] = 'Parents Email'; }else{ $this-&gt;embedRelation('Email'); $this-&gt;widgetSchema['Email']-&gt;setLabel('Parents Email'); $useFields[] = 'Email'; } </code></pre> <p>This works just fine if I am in the Parent Form but when I am in the Student Form (which embeds the parents form) it's not relating the email back to the parent, it creates the Email correctly in the <code>email table</code> but does not insert the email_id in the <code>parent table</code></p> <p>I'm going mad with this I just don't understand, please help!</p> <p><strong>IDIOT</strong></p> <p><strong>ANSWER</strong></p> <p>Turns out I had some old models that were linking tables that I thought a model rebuild would remove turns out not so much, got that removed and all the crazy weirdness is gone, things work perfect!</p> <p>Always something stupid that is missed</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload