繁体   English   中英

胖模型和瘦控制器设计控制器方法和模型方法之间的混淆

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM