Model Services question:
I’ve got the hang of Models and relationships in the new services, but I haven’t been able to figure out the best way to update a pivot table after deleting a related item.
For example: I have three tables: menu_items, item_options, and the pivot table menu_items_related.
I have models for the menu_items and item_options tables. It seems like if I have the $_relationships set up in each of the models, the pivot table should be updated when a row in either of the related tables is deleted, but that’s not happening.
Does anyone have this figured out? Can you share an example?
Thanks!
This appears to work…
In a model with the pivot table relationship declared,
class MenuItem extends Model {
protected static $_primary_key = 'menu_item_id';
protected static $_table_name = 'menu_items';
protected static $_relationships = array(
'ItemOption' => array(
'type' => 'HasAndBelongsToMany',
'pivot' => array('table'=>'pivot_table_name',
'left'=>'menu_item_id',
'right'=>'item_option_id'),
)
);
protected static $_events = array('beforeDelete');
protected $menu_item_id;
protected $item;
protected $description;
protected $price;
protected $notes;
public function onBeforeDelete()
{
$this->ItemOption = NULL;
$this->save();
}
}
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.