简体   繁体   English

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

[英]fat model and skinny controller design confusion between the controller method and model method

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 : 我是MVC的新手(使用codeIgniter作为我的例子)我已经读了MVC胖模型和瘦调控制​​器3次,我得到了:

  • model does the hardwork while controller calls the model and passes the data to be rendered by view 当控制器调用模型并传递要由视图呈现的数据时,模型会做出艰苦的工作

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): 但我有一个混乱,例如我有一个管理页面,将删除数据库中的产品数据,我会有这个代码(使用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? 在瘦控制器和胖模型的概念中,我应该将方法_deleteProductImages($id)到我的productModel,还是应该这样离开呢? 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). 此方法将执行删除产品所需的所有工作(包括删除关联的DB记录,文件等)。

The method would return TRUE if the operation was successful. 如果操作成功,该方法将返回TRUE。

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. 如果无法删除关联的记录或文件,我会在其操作中记录该错误,可能会在UI中引发错误消息并继续。

The method may call other methods in other models...for instance, I may have a product_attributes model that stores attributes for all products. 该方法可以调用其他模型中的其他方法...例如,我可能有一个product_attributes模型,用于存储所有产品的属性。 That model might have a method: delete_by_product_id(). 该模型可能有一个方法: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. 在这种情况下,我的产品模型将调用product_attributes-> delete_by_product_id(),它将处理相关记录的删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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