Personally, it would make me feel more comfortable if the parent table first got its ID, then that ID is used to populate the child table. That way there is less that can go wrong (e.g. race conditions).
So here's what I would do. Set the default value of the child table to whatever you like (or nothing if the field allows nulls). Then in the beforeInsert method, you set the ID of the child table to match that of the newly created parent record.
e.g.
child table delegate class:
- Code: Select all
function beforeInsert(&$record){
$parent = new Dataface_Record('parent_table', array());
$parent->setValues(array(
// .... The values that we're setting on the parent
));
$parent->save();
$record->setValue('ENTITY_ID', $parent->val('ENTITY_ID'));
}
Now you also need to think this through carefully. What happens if you insert a record into the child table and the corresponding parent record already exists? Can that ever happen? Make sure you take that into account for your solution.
-Steve