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:
So here is what my issue:
schema:
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
So in english, I have a student who has a parent(s) and that parent(s) has an email address, simple enough.
So in my Student Form I have the following:
//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)
}
This looks as expected and works for adding a Student record, however on update I get the error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2' for key 1
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
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!
~ Mahalo Zjoia
UPDATE
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:
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';
}
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 email table but does not insert the email_id in the parent table
I’m going mad with this I just don’t understand, please help!
IDIOT
ANSWER
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!
Always something stupid that is missed
Your problem is very common and described at the symfony website.
Here is example of what you need.
Also may be useful this documentation. "Especially Chapter 11 – Doctrine Integration".
Regards
Evgeny