Hi there.
In my app I have Category, Post and PostCategory model.
Category:
public function relations()
{
return array(
'products' => array(self::MANY_MANY, 'Product', 'tbl_product_category(category_id, product_id)'),
);
}
Product:
public function relations()
{
return array(
'categories' => array(self::MANY_MANY, 'Category', 'tbl_product_category(product_id, category_id)'),
);
}
ProductCategory:
public function relations()
{
return array(
'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
'product' => array(self::BELONGS_TO, 'Product', 'product_id'),
);
}
And when deleting one category I want to delete all products in it. This is how I’m trying to do it:
$transaction=Category::model()->dbConnection->beginTransaction();
try
{
$criteria=new CDbCriteria;
$criteria->condition='category_id=:category_id';
$criteria->params=array(':category_id'=>$id);
ProductCategory::model()->deleteAll($criteria);
$categories = Category::model()->with('products')->findByPk($id);
$categories->deleteAll();
$transaction->commit();
}
catch(Exception $e)
{
$transaction->rollback();
throw $e;
}
And I always get exception:
Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (sale-monster
.tbl_product_category
, CONSTRAINT tbl_product_category_ibfk_6
FOREIGN KEY (category_id
) REFERENCES tbl_category
(id
) ON DELETE NO ACTION ON UPDATE NO ACTION). The SQL statement executed was: DELETE FROM tbl_category
What am I doing wrong? Should I use "on delete cascade" option to perform this deletion, if so, please help me to do it.
Thanks a lot in advance, Max.