贴一个model类的写法,求大牛看看是否可行
本帖最后由 nicolaslei 于 2011-12-10 18:55 编辑首先是自定义的扩展model父类
class MY_Model extends CI_Model{
//用于存放字段数据获取值,在更新或添加时使用
protected $column_values = array ();
//标识:标识是否在 __set 的时候给 column_values 填充数据
protected $column_flag = FALSE;
/* limit */
protected $page;
protected $page_size;
protected $total_count;
/* end */
/**
* 表名 (不带前缀)
*
* @var string
*/
public $table_name;
function __construct()
{
parent::__construct();
$this->page = 1;
$this->page_size = 20;
$this->total_count = 0;
}
/**
* 魔术函数
* 给私有变量设值
*
* @param string $k
* @param int/string $v
* @return void
*/
function __set($k, $v)
{
/**
* page/pageSize/totalCount/id 将不会被记录到column_values中
*/
$not_column_values = array('column_flag', 'page', 'page_size', 'total_count', 'id');
if ($this->column_flag && ! in_array($k, $not_column_values))
{
$this->column_values [$k] = $v;
}
$this->$k = $v;
}
/**
* 获取总记录数
*
* @access public
*/
function get_total_count()
{
return $this->total_count;
}
/**
* 新增
*
* @access protected
* @return boolean - success/failure
*/
protected function insert()
{
$this->db->insert($this->table_name, $this->column_values);
return $this->db->insert_id();
}
/**
* 移出
*
* @param string $field 条件字段
* @access protected
* @return boolean - success/failure
*/
protected function remove($field = NULL){
if( $field == NULL )
{
$field = 'id';
}
$this->db->where($field, intval($this->{$field}))->delete($this->table_name);
return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
}
/**
* 更新
*
* @access protected
* @param string $upd_field_key 要更新的条件字段
* @return boolean - success/failure
*/
protected function update($field = NULL)
{
if( $field == NULL )
{
$field = 'id';
}
$this->db->where($field, $this->{$field});
$this->db->update($this->table_name, $this->column_values);
return ($this->db->affected_rows() == 1)?TRUE:FALSE;
}
/**
* 获取
*
* @access protected
* @param string $field 要获取的条件字段
* @return array
*/
protected function get_one($field = NULL)
{
if( $field == NULL )
{
$field = 'id';
}
$data = array();
$this->db->select('*')->from($this->table_name)->where($field, $this->{$field})->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1)
{
$data = $query->row_array();
}
$query->free_result();
return $data;
}
}
/* End of file MY_Model.php */
/* Location: ./application/core/MY_Model.php */
具体的model类,继承于MY_model
class Product_log_m extends MY_Model{
/**
* 属性必须定义为protected,否则无法设值
*/
protected $id;
protected $product_id;
protected $user_id;
protected $action_type;
protected $create_time;
function __construct()
{
parent::__construct();
/*设置表名,不带前缀*/
$this->table_name = 'product_log';
}
/**
* 添加产品产品操作日志
*
* @access public
* @return boolean - success/failure
*/
function add()
{
$this->column_values['create_time'] = time();
return $this->insert();
}
/**
* 根据产品编号移出产品操作日志
*
* @access public
* @return boolean - success/failure
*/
function remove_by_product_id(){
return $this->remove('product_id');
}
/**
* 根据编号移出产品操作日志
*
* @access public
* @return boolean - success/failure
*/
function remove_by_id()
{
return $this->remove('id');
}
/**
* 根据编号ID移出产品操作日志
*
* @access public
* @return boolean - success/failure
*/
function update_by_id()
{
return $this->update('id');
}
/**
* 根据编号ID获取一个数据
*
* @access public
* @return array
*/
function get_one_by_id()
{
return $this->get_one('id');
}
/**
* 获取产品操作日志列表
*
* @access public
* @return array - 产品产品操作日志信息
*/
function get_product_logs()
{
return $this->db->get($this->table_name);
}
}
/* End of file prodcut_log_m.php */
/* Location: ./application/models/prodcut_log_m.php */
控制器操作
class Product_log extends Hzb_M_Controller {
/**
* 传递到对应视图的数据
*
* @access private
* @var array
*/
private $_data = array();
function __construct()
{
parent::__construct();
//加载配置文件
$this->load->config('hzb');
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('product_log_m', 'pro_log');
}
/**
* 默认执行函数
*
* @access public
* @return void
*/
function index()
{
$page = $this->input->post('pageNum');
if ( ! $page )
{
$page = 1;
}
$this->pro_log->page = $page;
$this->_data['results'] = $this->pro_log->get_product_logs_limit();
$this->_data['numPerPage'] = '20';
$this->_data['totalCount'] = $this->pro_log->get_total_count();
$this->_data['currentPage'] = $page;
$this->load->view($this->config->item('manager_folder').'/user_list',$this->_data);
}
/**
* 新增
*
* @access public
* @return void
*/
function add()
{
$this->pro_log->column_flag = TRUE;
$this->pro_log->product_id = $this->input->post('product_id');
$this->pro_log->user_id = $this->input->post('user_id');
$this->pro_log->action_type = $this->input->post('action_type');
$this->pro_log->add();
m_message_json('200', '添加成功!', 'user_list');
}
/**
* 修改
*
* @access public
* @return void
*/
function update()
{
$this->pro_log->id = $this->input->post('id');
$this->pro_log->column_flag = TRUE;
$this->pro_log->product_id = $this->input->post('product_id');
$this->pro_log->user_id = $this->input->post('user_id');
$this->pro_log->action_type = $this->input->post('action_type');
$this->pro_log->update_by_id();
m_message_json('200', '修改成功!', 'user_list');
}
function del($id)
{
$this->pro_log->id = $id;
$this->pro_log->remove_by_id();
m_message_json();
}
/**
* 批量删除
*
* @access public
* @return void
*/
function del_batch()
{
$ids = $this->input->post('ids', TRUE);
foreach ($ids as $id)
{
$this->pro_log->id = $id;
$this->pro_log->remove_by_id();
}
m_message_json();
}
}
/* End of file product_log.php */
/* Location: ./application/controllers/admin/product_log.php */
第一次写技术贴,有什么不足,请多指正
谢谢老大 Hex 帮我移动板块,我是论坛新人,对于一些帖子发在哪里合适,分得不太清楚{:soso_e100:}
沙发我要了 学习中, 虽然看不太懂.. 不过还是要顶 {:soso_e149:}就一同学关注吖,是不是标题不响亮 LZ代码写得很漂亮 功能也很实用 赞一个{:soso_e100:} {:1_1:}谢谢LS的肯定 没看懂,不知道楼主是基于什么需求需要重写Model和Controller
不过楼主的代码写得比较严谨和规范,看上去很是舒服……
坐等大牛评论…… 回LS的,重写model,主要是为了一些代码能够重用,一些不很复杂的方法,可以在MY_Model的基础方法上进行扩展,减少一些重复的代码 nice,你可以写个自动从数据库生成基础model的程序,里面包含字段信息,表名等
比如
class BaseUsersModel extends MY_Model {
var $pk='id';
var $tablename='fp_users';
var $fields=array(
array(
'name' =>'id',
'type' =>'int',
'length' =>10,
'autoinc' =>TRUE,
'default' =>'',
'null' =>FALSE,
'unique' =>TRUE,
'comment' =>'',
),
array(
'name' =>'users_name',
'type' =>'varchar',
'length' =>15,
'autoinc' =>FALSE,
'default' =>'',
'null' =>FALSE,
'unique' =>FALSE,
'comment' =>'用户名',
),
array(
'name' =>'users_email',
'type' =>'varchar',
'length' =>32,
'autoinc' =>FALSE,
'default' =>'',
'null' =>FALSE,
'unique' =>FALSE,
'comment' =>'邮箱',
),
array(
'name' =>'add_time',
'type' =>'int',
'length' =>10,
'autoinc' =>FALSE,
'default' =>'',
'null' =>FALSE,
'unique' =>FALSE,
'comment' =>'',
),
);
}
然后具体的业务逻辑可以写在继承自这个类的model里面,比如
class UserModel extends BaseUsersModel
{
//your code
}
这样如果数据库修改了,重新生成一遍base类即可,而且base类里面的数据可以做更多其他的用途,比如验证等等,当然base类是不可以人工修改的。
页:
[1]