[英]fat model and skinny controller design confusion between the controller method and model method
我是MVC的新手(使用codeIgniter作為我的例子)我已經讀了MVC胖模型和瘦調控制器3次,我得到了:
但我有一個混亂,例如我有一個管理頁面,將刪除數據庫中的產品數據,我會有這個代碼(使用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);
}
}
我的問題是:
在瘦控制器和胖模型的概念中,我應該將方法_deleteProductImages($id)
到我的productModel,還是應該這樣離開呢? 如果您有其他更好的方法,那么請在這里指導我
我會在我的模型中有一個刪除產品的方法。 此方法將執行刪除產品所需的所有工作(包括刪除關聯的DB記錄,文件等)。
如果操作成功,該方法將返回TRUE。
如果無法刪除關聯的記錄或文件,我會在其操作中記錄該錯誤,可能會在UI中引發錯誤消息並繼續。
該方法可以調用其他模型中的其他方法...例如,我可能有一個product_attributes模型,用於存儲所有產品的屬性。 該模型可能有一個方法:delete_by_product_id()。 在這種情況下,我的產品模型將調用product_attributes-> delete_by_product_id(),它將處理相關記錄的刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.