I have a situation where I have parent/child tables.
Parent
-
id (PK - related to parentid in child table)
-
parentname (same as child.childname)
Child
-
id
-
parentid
-
otherid (relationship to another table)
-
childname
the form I have created collects data for the child form.
Without going into the various reasons why this is the way it is just trust me that this is the way it needs to be. the structure isnt the issue here.
the childname field in the form is an autocomplete field where the user can select an existing childname (from the list of parentnames) or write in a new one. A child name selected from autocomplete assigns its db id (the parentid) to the parentid field in the form. if it does not exist, the value assigned is -1.
if the childname property does not exist in the parent (ie: parentid = -1), the parent record needs to be created, the new ID retrieved, then used as a relational join in the new child record.
the child table also has a field called "otherid" that does not exist in the parent table but is collected in the form.
if the child name exists in the parent table then no parent record needs to be created, just the child record is to be created using the existing parentid.
So, in the childController.actionCreate function I have the following code:
$model=new child;
if(isset($_POST['child']))
{
$child = $_POST['child'];
$parentid = $child['parentid'];
// if less than 0, parent record needs to be created
if($parentid<0)
{
$parentmodel = new parent;
$parentmodel->parentname = $_POST['child_childname'];
ERROR HERE --> $parentmodel->save(false);
// put the ID into the child array
$child['parentid'] = $parentmodel->id;
}
$model->attributes=$child;
$model->save();
}
This code produces an error that says "Property parent.otherid is not defined".
which is 100% correct, parent.otherid does not exist. it only exists in the child table. so even though I am specifying the parent model in the error line, it ends up assuming the controller for the child.
How can I save an item to the parent table from the childController.actionCreate function?
thanks in advance
Greg J