Hi guys,
I have created a controller that saves or updates Product along with its stock and attributes. Each product has one stock level and multiple attributes. I am trying to save everything by just saving $product, like in the documentation. However, if I modify my code like in the example below, no related records are created:
// Create an artist
$artist = new Artists();
$artist->name = 'Shinichi Osawa';
$artist->country = 'Japan';
// Create an album
$album = new Albums();
$album->name = 'The One';
$album->artist = $artist; // Assign the artist
$album->year = 2008;
$album->save();
The only way to create/update my related records is by saving the main record (products) and then using the id to save the related records in separate transactions. Here is part of my Products model:
$this->hasMany('id', __NAMESPACE__ . '\ProductAttributes', 'product_id', array(
'alias' => 'ProductAttributes',
'foreignKey' => array(
'action' => Relation::ACTION_CASCADE
)
));
$this->hasOne('id', __NAMESPACE__ . '\ProductStock', 'product_id', array(
'alias' => 'ProductStock',
'foreignKey' => array(
'action' => Relation::ACTION_CASCADE
)
));
And here are parts of my stock and attributes models:
$this->belongsTo('product_id', _NAMESPACE_ . '\Products', 'id', array(
'alias' => 'Products'
));
$this->belongsTo('product_id', _NAMESPACE_ . '\Products', 'id', array(
'alias' => 'Products'
));
It does not really bother me to save related records separately, but I would like to know what I am missing here...
Thanks!