I have two tables. One is category and that category has MANY nominations.
Table Category
protected static $_has_many = array(
'nominations' => array(
'key_from' => 'id',
'key_to' => 'category_id',
'model_to' => 'Model_Nominations',
'cascade_save' => true,
'cascade_delete'=> true
)
);
Table Nominations
protected static $_belongs_to = array(
'categories' => array(
'key_from' => 'category_id',
'key_to' => 'id',
'model_to' => 'Model_Categories',
'cascade_save' => true,
'cascade_delete'=> true
)
);
When I delete the categories:
public static function delete_($args)
{
$q = Model_Categories::query()
->where('id','=',$args['id']);
if($q->count() > 0){
$q->delete();
return true;
}
return false;
}
It doesn't delete the nomination entries in the Nominations table even with the cascade_delete
set to true. What did I miss?
Your logic is wrong.
In your example $q
is an ORM query object, so that delete just runs a delete query: DELETE FROM categories WHERE id = ?
.
You need to fetch a model object with it's relations if you want to delete an object and it's relations. It will not cascade into relations that are not loaded.
public static function delete_($args)
{
if ($result = Model_Categories::find($args['id'], array('related'=>array('nominations')))
{
return $result->delete();
}
return false;
}