douguanyan9928 2012-08-11 22:28
浏览 24
已采纳

CodeIgniter的示例模型中的属性是什么?

In the CodeIgniter User Guide, the example model had an attribute for each field in the corresponding database table:

    class Blogmodel extends CI_Model {

        var $title   = '';
        var $content = '';
        var $date    = '';
        //...

However when it returns a set of blog posts, they are only generic object.

    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

If each blog post is only going to be a generic object, what is the purpose of the attributes at the beginning of the class?

It seems like it would be nice to have each blog post as a fully fledged model object populated with it's content. Then you could write methods that fetched values internally. However, all the examples in the User Guide seem to show each controller having one instance of each model and using that object to fetch generic objects containing the database values. Additionally, the way the examples instantiate the models ($this->load->model('Blog');) it's clear that you are only expected to have one instance of the model within the controller.

If my blog post has a status value which is stored in the database as an integer, but in several different controllers I want to show that value as a human readable string, where do I put that function/method? Since I need it in multiple controllers, and it's specific to one object, I assume it should be a method of the model object. If each blog post is an instance of the model, I could write it like this:

    function get_human_status()
    {
        $lookup = array(
            1 => 'Active',
            2 => 'Hidden',
            3 => 'Pending');
        return $lookup[$this->status];
    }

However, if each blog post is a generic object it would have to look like this:

    function get_human_status($data)
    {
        $lookup = array(
            1 => 'Active',
            2 => 'Hidden',
            3 => 'Pending');
        return $lookup[$data->status];
    }

Which way is the more idiomatic of CodeIgniter? Assuming it's the second, why does the example model define those internal attributes?

Finally, for bonus points, if I had other questions of this nature, what books, articles, or examples should I be looking at?

  • 写回答

1条回答 默认 最新

  • dongshou7903 2012-08-11 22:57
    关注

    Ehh .. the disaster that goes by name "CodeIgniter".

    In the documentation of that abysmal framework, they portray "model" as instance which implements active record pattern. This is why you get the pointless attributes in the definition of that class. It is just another side-effect of conflict between two responsibilities (storage and logic) withing AR pattern. Also, you might notice the var keyword, which is an artifact of PHP4.

    What you call "models" are not. They are simple domain objects, which in turn are part of model later. Model layer is a mental construct, just like the other layer in MVC design pattern - presentation layer.

    The $this->load->model() method does not initialized "model" instances. It is just another example of CodeIgniters lack of proper autoloader. This method is there just to include the file, which contains the Blog class. Therefor, you cannot conclude that "Blog" i supposed to be a singular object.

    Controllers a not supposed to "show" anything. There are there to change the state of current view instance and the model layer. Views are supposed to deal with presentation logic choose how to display something and which templates to use. Views are not supposed to be dumb templates.

    As for educational materials, you will find a lost of lectures and books at the end of this post. You might also this and this somewhat relevant.

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算