CI框架篇之模型篇--初识(1)

模型

模型是专门用来和数据库打交道的PHP类。例如,假设你想用CodeIgniter来做一个Blog。

你可以写一个模型类,里面包含插入、更新、删除Blog数据的方法。

  下面的例子将向你展示一个普通的模型类:

class Blog_model extends CI_Model {

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

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

    function insert_entry()
    {
        $this->title   = $_POST['title']; // 请阅读下方的备注
        $this->content = $_POST['content'];
        $this->date    = time();

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

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

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

}

   上面展示的是CI里面的AR数据库操作

使用模型

模型可以在 控制器 中被引用。 就像这样:

$this->load->model('table_name/Model_name');

模型一旦被载入,你就能通过下面的方法使用它:

调用的方法是调用你写的模型,然后调用该模型下的方法,去使用模型

$this->Model_name->function();

原文地址:https://www.cnblogs.com/sunxun/p/3776701.html