---------------------------------------------------------------------------------------------------------
使用CI以來最強烈的感受是其徹底的MVC設計, 舉個例子 : 在application/modesl目錄裡, 寫我們的模型操作, 統一繼承CI_Model.
而在控制器裡只寫邏輯, 無法直接操作數據庫, 需要數據直接調用模型, 最後是調用模板.
下面分別展示模型, 控制器, 和視圖間的協作.
/** * 用戶模型, 完整CURD示例 * @Chenwei */ class User_model extends CI_model
{ public function __construct() { parent::__constrcut(); } /** * 查詢用戶信息, 這裡不建議使用單一id參數作為條件, 為了便於控制器自己組裝條件復用此模型方法 * @param array 格式如: $where = array('id'=>1); * @return array */ public function userInfo($where = array()) { if($where && is_array($where)) { $res = $this->db->select('id, username, age')->where($where)->get('users'); return $res->result_array(); //以二維數組形式返回結果 } else {
$res = $this->db->select('id, username, age')->get('users');
return $res->result_array(); } } /** * 添加用戶 * @param array 格式如: $data = array('username'=>'Chenwei', 'age'=>'18'); * @reteurn bool */ public function userAdd($data) { if($data && is_array($data)) { $bool = $this->db->insert('users', $data); return $bool; } else { return false; } } /** * 刪除用戶 * @param int $id * @reteurn bool */ public function userDel($id) { if($id) { $where = array('id'=>$id); $bool = $this->db->where($where)->delete('users'); return $bool; } else { return false; } } /** * 修改用戶 * @param array $where 條件 * @param array $data 新數據 * @reteurn bool */ public function userEdit($where, $data) { if($where && is_array($where)) { $bool = $this->db->where($where)->update('users', $data); return $bool; } else { return false; } } } /** * 幾點注意: * 1. 模型類名字User_model首字母大寫, 其余字母小寫, 繼承基礎模型類CI_Model * 2. 類文件名 application/models/user_model.php * 3. 控制器中如何載入此模型 :
$this->load->model('User_model', 'user'); 這是以user為對象名引入;
$this->load->model('User_model'); 這是默認以User_model為對象名引入. 模型文件支持子目錄;
如果類文件在application/models/blog/user_model.php中, 可以這樣引入: $this->load->model('blog/User_model'); * 4. 如果有需要, 你可以設置自動加載, 在 application/config/autoload.php文件中.
* 5. 如果沒有設置自動連接數據庫, 加在模型的時候可以設置連接, 像這樣 $this->load->model('User_model', '', TRUE); */
Ps:
這裡是一個聯合查詢的例子, 有需要可以嘗試:
$res = $this->db->select('p.id, p.uid, p.order_no, p.amount, p.pay_way, p.pay_type, p.pay_bank, p.pay_time, p.goods_type, p.contact_tel, p.detail_desc, p.add_time, u.username')->from('payment as p')->join('users as u', 'p.uid = u.id')->order_by('p.id', 'desc')->get();
/**
* 用戶控制器, CURD示例
* @Chenwei
*/
class Users extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('User_model', 'user');
}
/**
* 用戶列表
*/
public function index()
{
$data['user_list'] = $this->user->userInfo();
$this->load->view('user_list', $data); //調用模板, 並將數據輸出到前台
}
/**
* 添加用戶
*/
public function user_add()
{
$data = array(
'username'=>$this->input->post('name');
'age'=>intval($this->input->post('age'));
);
$bool = $this->user->userAdd($data);
if($bool)
{
$this->show_tips('操作成功 !');
}
else
{
$this->show_tips('操作失敗 !');
}
}
/**
* 修改用戶
*/
public function user_edit()
{
$id = $this->input->post('id');
$data = array(
'username'=>$this->input->post('name');
'age'=>intval($this->input->post('age'));
);
if($id)
{
$where = array('id'=>$id);
$bool = $this->user->userEdit($where, $data);
if($bool)
{
$this->show_tips('操作成功 !');
}
else
{
$this->show_tips('操作失敗 !');
}
}
else
{
$this->show_tips('非法操作 !');
}
}
/**
* 刪除用戶
*/
public function user_del()
{
$id = $this->input->post('id');
$bool = $this->user->userDel($id);
if($bool)
{
$this->show_tips('操作成功 !');
}
else
{
$this->show_tips('操作失敗 !');
}
}
}
/**
* 幾點注意:
* 1. 控制器文件在 application/controller/users.php , 支持子目錄
* 2. 控制器名首字母必須大寫, 且必須繼承CI_Controller
* 3. 前後台權限控制都在application/core/MY_Controller.php文件中,
定義兩個控制器, 分別用於前台和後台, 繼承CI_Controller , 其余都只需繼承這兩個自定義的控制器即可.
* 4. 定義默認控制器, 在 application/config/route.php
*/
/**
* 視圖層 示例
* @Chenwei
*/
<?php
$this->load->view('header');
?>
<!-- 簡單的輸出 -->
<div>
<table>
<?php if($user_list):?>
<?php foreach($user_list as $v):?>
<tr><td><?=$v['username'];?></td></tr>
<?php endforeach;?>
<?php endif;?>
</table>
</div>
<?php
$this->load->view('header');
?>
/**
* 幾點注意:
* 1. 模板中可以直接使用控制器中分配的變量, 使用CI系統的所有函數和方法.
* 2. 開啟CI短標簽支持後, 即使php未開啟支持, CI也會幫我們自動解析, 可以放心使用.
*/
可能存在手誤, 以上Code不要直接復制使用; 更多CI的實用用法, 可以隨時去查閱CI手冊.
Link: http://www.cnblogs.com/farwish/p/3991419.html
@黑眼詩人 <www.chenwei.ws>