I compile PHP Active Record and do associations. I have two related objects using "Has_Many" and "Belongs_to" (parent / child) and trying to create a child entry when creating a new parent record (in this case, a "skin" is created for my "unit").
class Unit extends ActiveRecord\Model { static $has_many = array( array('skins') ); } class Skin extends ActiveRecord\Model { static $belongs_to = array( array('unit') ); }
I found both of these threads here: http://www.phpactiverecord.org/boards/4/topics/153 Activerecord-association: create a new object (find a class)
So now my code is as follows:
$unit = new Unit(); $unit->name = 'somename'; $unit->description = 'somedescription'; $skinArray = array('name' => $unit->name.' Default Skin'); $unit->create_skins($skinArray); $unit->save();
The code above does not associate the new skin with a block in the database or in the code, although it / is / places the new skin record in the database (with the unit_NULL). Using build_skins does not put the Skin entry in the database.
I was hoping there was a way to add a βchildβ to the parent model using the model itself, as some other ORM does. The only way to do this is to do it explicitly:
$unit = new Unit(); $skin = new Skin(); $unit->name = 'somename'; $unit->description = 'somedescription'; $unit->save(); $skin->unit_id = $unit->id; $skin->name = $unit->name.' Default Skin'; $skin->save();
Perhaps this should be done in PHP ActiveRecord, and my expectations are wrong. But I was hoping to find a way to do this through objects that do not require saving the parent in the database with an explicit call. For example, in the PHP infrastructure "Recess" there would be a simple call on such a device: $ unit-> addSkin ($ skin);
source share