CodeIgniter 模型

2018-07-21 15:37 更新

模型

模型對(duì)于那些想使用更傳統(tǒng)的 MVC 模式的人來(lái)說(shuō)是可選的。

目錄

什么是模型?

模型是專(zhuān)門(mén)用來(lái)和數(shù)據(jù)庫(kù)打交道的 PHP 類(lèi)。例如,假設(shè)你使用 CodeIgniter 管理一個(gè)博客,那么你應(yīng)該會(huì)有一個(gè)用于插入、更新以及獲取博客數(shù)據(jù)的模型類(lèi)。 這里是一個(gè)模型類(lèi)的例子:

class Blog_model extends CI_Model {

    public $title;
    public $content;
    public $date;

    public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
    }

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

    public function insert_entry()
    {
        $this->title    = $_POST['title']; // please read the below note
        $this->content  = $_POST['content'];
        $this->date = time();

        $this->db->insert('entries', $this);
    }

    public function update_entry()
    {
        $this->title    = $_POST['title'];
        $this->content  = $_POST['content'];
        $this->date = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}

注解
上面的例子中使用了 查詢構(gòu)造器 數(shù)據(jù)庫(kù)方法。

注解
為了保證簡(jiǎn)單,我們?cè)谶@個(gè)例子中直接使用了 $_POST 數(shù)據(jù),這其實(shí)是個(gè)不好的實(shí)踐, 一個(gè)更通用的做法是使用 輸入庫(kù) 的$this->input->post('title')。

剖析模型

模型類(lèi)位于你的 application/models/ 目錄下,如果你愿意,也可以在里面創(chuàng)建子目錄。

模型類(lèi)的基本原型如下:

class Model_name extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }

}

其中,Model_name 是類(lèi)的名字,類(lèi)名的第一個(gè)字母 必須 大寫(xiě),其余部分小寫(xiě)。確保你的類(lèi) 繼承 CI_Model 基類(lèi)。

文件名和類(lèi)名應(yīng)該一致,例如,如果你的類(lèi)是這樣:

class User_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
    }

}

那么你的文件名應(yīng)該是這樣:

application/models/User_model.php

加載模型

你的模型一般會(huì)在你的 控制器 的方法中加載并調(diào)用, 你可以使用下面的方法來(lái)加載模型:

$this->load->model('model_name');

如果你的模型位于一個(gè)子目錄下,那么加載時(shí)要帶上你的模型所在目錄的相對(duì)路徑, 譬如,如果你的模型位于 application/models/blog/Queries.php, 你可以這樣加載它:

$this->load->model('blog/queries');

加載之后,你就可以通過(guò)一個(gè)和你的類(lèi)同名的對(duì)象訪問(wèn)模型中的方法。

$this->load->model('model_name');

$this->model_name->method();

如果你想將你的模型對(duì)象賦值給一個(gè)不同名字的對(duì)象,你可以使用 $this->load->model() 方法的第二個(gè)參數(shù):

$this->load->model('model_name', 'foobar');

$this->foobar->method();

這里是一個(gè)例子,該控制器加載一個(gè)模型,并處理一個(gè)視圖:

class Blog_controller extends CI_Controller {

    public function blog()
    {
        $this->load->model('blog');

        $data['query'] = $this->blog->get_last_ten_entries();

        $this->load->view('blog', $data);
    }
}

模型的自動(dòng)加載

如果你發(fā)現(xiàn)你有一個(gè)模型需要在整個(gè)應(yīng)用程序中使用,你可以讓 CodeIgniter 在系統(tǒng)初始化時(shí)自動(dòng)加載它。打開(kāi) application/config/autoload.php 文件, 并將該模型添加到 autoload 數(shù)組中。

連接數(shù)據(jù)庫(kù)

當(dāng)模型加載之后,它 并不會(huì) 自動(dòng)去連接你的數(shù)據(jù)庫(kù),下面是一些關(guān)于 數(shù)據(jù)庫(kù)連接的選項(xiàng):

  • 你可以在控制器或模型中使用 標(biāo)準(zhǔn)的數(shù)據(jù)庫(kù)方法 連接數(shù)據(jù)庫(kù)。

  • 你可以設(shè)置第三個(gè)參數(shù)為 TRUE 讓模型在加載時(shí)自動(dòng)連接數(shù)據(jù)庫(kù),會(huì)使用你的數(shù)據(jù)庫(kù)配置文件中的配置:

    $this->load->model('model_name', '', TRUE);
  • 你還可以通過(guò)第三個(gè)參數(shù)傳一個(gè)數(shù)據(jù)庫(kù)連接配置:

    $config['hostname'] = 'localhost';
    $config['username'] = 'myusername';
    $config['password'] = 'mypassword';
    $config['database'] = 'mydatabase';
    $config['dbdriver'] = 'mysqli';
    $config['dbprefix'] = '';
    $config['pconnect'] = FALSE;
    $config['db_debug'] = TRUE;
    
    $this->load->model('model_name', '', $config);
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)