php ci框架之创建mobel

来源:互联网 发布:linux 倒序查看文件 编辑:程序博客网 时间:2024/05/21 03:56

ci规定model的命名格式为XXX_model.php,model类名的命名格式为文件名第一个字母大写;如blog_model.php->class Blog_model extends CI_Model ;所有的model都继承自CI_Model;在model中如果配置文件autoload.php中没有配置$autoload['libraries'] = array('database');那么你就得先手动载入数据库类database;

 public function __construct()  {    $this->load->database();  }
$this->db->get();运行选择查询语句并且返回结果集。可以获取一个表的全部数据 

$this->db->select();允许你在SQL查询中写 SELECT 部分

$this->db->from();允许你编写查询中的FROM部分

$this->db->join();允许你编写查询中的JOIN部分

$this->db->where();允许你设置 WHERE 子句

$this->db->like();允许你生成 LIKE 子句等等;

具体可参考ci框架包解压出来的user_guide文件夹下的database/active_record.html


class Blog_model extends CI_Model 
{
var $table = "blog";

/**
*获取所有分类
*
*/
function getAll()
{
return $this->db
->order_by("px")
->get($this->table)
->result_array();
}

/**
**获取一条分类
**
**/
function getOne($id)
{
return $this->db->from($this->table)
->where("id", $id)
->get()
->row_array();
}

/**
**添加分类
**
**/
function add()
{
$data = array(
'title' =>  $this->input->post('title'),
'cid' =>  $this->input->post('cid'),
'contents' =>  $this->input->post('contents'),
'px' =>  $this->input->post('px'),
);
$this->db->insert($this->table, $data);
}

/**
**修改分类
**
**/
function edit()
{
$data = array(
'title' =>  $this->input->post('title'),
'cid' =>  $this->input->post('cid'),
'contents' =>  $this->input->post('contents'),
'px' =>  $this->input->post('px'),
);
$id = $this->input->post('id');
$this->db->where('id', $id)->update($this->table, $data);
}

/**
**删除分类
**
**/
function del()
{
$id = $this->uri->segment(4);
$this->db->where('id',$id)->delete($this->table);
}


}

0 0