模型

模型是专门用来和数据库打交道的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();

相关文章:

  • 2021-04-14
  • 2021-10-09
  • 2021-07-23
  • 2022-12-23
  • 2021-11-22
  • 2022-01-17
  • 2021-08-15
猜你喜欢
  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2021-09-30
  • 2021-10-22
  • 2022-12-23
相关资源
相似解决方案