php ci框架之创建mobel
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);
}
}
總結
以上是生活随笔為你收集整理的php ci框架之创建mobel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蛮力法之顺序查找
- 下一篇: 线搜索中的Armijo-Goldstei