duan010167787 2013-03-22 14:03
浏览 131
已采纳

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

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 :

  • 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):

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

  • 写回答

1条回答 默认 最新

  • douzen1896 2013-03-22 14:21
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输