为什么要添加$ this->模型。让它更快?

时间:2009-09-26 13:06:43

标签: php codeigniter

我正在玩这个代码。 http://programmersvoice.com/tag/code 我注意到以下几行。

$this->load->model($this->models."pagemodel", 'pages');

我将其与

进行比较
$this->load->model("pagemodel", 'pages');

这是codeigniter的文档http://codeigniter.com/user_guide/general/models.html#loading建议的内容。

然而,方法2比第一个花费更长的时间。

任何人都可以解释“$ this-> model”。好吗?

提前致谢。

以下是controllers / admin

中pages.php的完整代码
<?php

class Pages extends Application
{

    function Pages()
    {
        parent::Application();
        $this->auth->restrict('editor'); // restrict this controller to editor and above
        $this->load->model($this->models."pagemodel", 'pages'); // Load the page model
    }

    function manage()
    {
        $data = $this->pages->pages(); // List the pages
        $this->table->set_heading('Title', 'Slug', 'Actions'); // Setting headings for the table

        foreach($data as $value => $key)
        {
            $actions = anchor("admin/pages/edit/".$key['id']."/", "Edit") . anchor("admin/pages/delete/".$key['id']."/", "Delete"); // Build actions links
            $this->table->add_row($key['title'], $key['slug'], $actions); // Adding row to table
        }

        $this->auth->view('pages/manage'); // Load the view
    }

    function delete($id)
    {
        $this->pages->delete($id);
        $this->auth->view('pages/delete_success');
    }

    function add()
    {

        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', 'Page Title', 'required');
        $this->form_validation->set_rules('content', 'Content', 'required');

        if($this->form_validation->run() == FALSE)
        {
            $this->auth->view('pages/add');
        }
        else
        {
            $data['title'] = set_value('title');
            $data['content'] = set_value('content');
            $data['slug'] = url_title($data['title'], 'underscore', TRUE);

            $this->pages->add($data);

            $this->auth->view('pages/add_success');
        }
    }

    function edit($id)
    {

        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('title', 'Page Title', 'required');
        $this->form_validation->set_rules('content', 'Content', 'required');

        if($this->form_validation->run() == FALSE)
        {
            $data = $this->pages->page($id);

            $this->auth->view('pages/edit', $data[0]);
        }
        else
        {
            $data['title'] = set_value('title');
            $data['content'] = set_value('content');
            $data['slug'] = url_title($data['title'], 'underscore', TRUE);

            $this->pages->edit($id, $data);

            $this->auth->view('pages/edit_success');
        }
    }
}

?>

1 个答案:

答案 0 :(得分:7)

我不完全确定以下内容,因为当前版本的Codeigniter似乎没有填充$this->models变量,但我认为:

$this->models包含应用程序模型目录的完整路径,因此加载速度更快,因为CI不必查看不同的文件夹(全局和应用程序)