i'm newbie in MVC (using codeIgniter as my example) and i have read MVC fat model and skinny controller for like 3 times, what i got :
but i have one confusion , example i have an admin page that would delete product data in the db, i would have this codes (using codeIgniter):
public function deleteProduct($id = '')
{
if( is_digit($id))
{
$this->load->model('productModel');
$this->productModel->deleteById($id);
//oops product has images in another DB table and in server, so i need to delete it
$success = $this->_deleteProductImages($id);
}
else
{
//redirect because of invalid param
}
//if success TRUE then load the view and display success
//else load the view and display error
}
protected function _deleteProductImages($productId)
{
$this->load->model('productModel');
//return array of images path
$imgs = $this->productModel->getImagesPath($productId);
// after i got the imgs data, then delete the image in DB that references to the $productId
$this->productModel->deleteImage($productId);
foreach($imgs as $imgPath)
{
if(file_exists $imgPath) unlink($imgPath);
}
}
my question is :
in the concept of thin controller and fat model, should i move the method _deleteProductImages($id)
to my productModel or should i leave it like that? if you have another better approach then please guide me here
I would have a method in my model for the deletion of products. This method would do ALL of the work required to delete a product (including deleting associated DB records, files, etc).
The method would return TRUE if the operation was successful.
In the event that an associated record or file couldn't be deleted, I'd log that error in it's operation, possibly raise an error message in the UI and continue.
The method may call other methods in other models...for instance, I may have a product_attributes model that stores attributes for all products. That model might have a method: delete_by_product_id(). In that case, my product model would call product_attributes->delete_by_product_id(), which would handle the deletion of the associated records.