分享一个自己封装的MY_Model,看来还是很多人需要的
分享一个自己封装的MY_Model,看来还是很多人需要的
赞。。。。。。但是,大概说说里面有啥吧 :lol 本帖最后由 Aloghli 于 2016-8-26 10:44 编辑
class MY_Model extends CI_Model {
public $table = '';
private $primary_key = 'id';
private $filter = 'intval';
private $_fields = '*';
private $_order = 'id desc';
private $_where = '';
private $_limit = '';
public function ____construct() {
parent::__construct();
}
/*不用在意但是也不要删掉,这是判断数组是不是多维数组的*/
public function isMultiArray($array) {
foreach ($array as $v)
if (is_array($v))
return TRUE;
return FALSE;
}
/*获取数据 *sid为数字返回单行数据 * sid位数组的返回多行数据 * 如果single设置位true也返回单行数据 * Object信息返回数据 */
public function get($sid = NULL, $single = FALSE) {
if (is_array($sid)) {
$this->where($sid);
$method = 'result';
} elseif (is_numeric($sid)) {
$filter = $this->filter;
$this->where($sid);
$method = 'row';
} elseif ($single) {
$method = 'row';
} else {
$method = 'result';
}
$this->order_by();
return $this->db->get($this->table)->$method();
}
/*
*这个方法跟上面的get方法用法一样,只不过这是数组形式返回数据 * */
public function get_as_array($sid = NULL, $single = FALSE) {
if (is_array($sid)) {
$this->where($sid);
$method = 'result_array';
} elseif (is_numeric($sid)) {
$filter = $this->filter;
$this->where($sid);
$method = 'row_array';
} elseif ($single) {
$method = 'row_array';
} else {
$method = 'result_array';
}
return $this->db->get($this->table)->$method();
}
/*
* 保存数据 * data必须位数组,多维数组也可以 * id空位插入。否则更新 * */
public function save($data, $id = NULL) {
if (isset( $id )) {
if (is_array($id)) {
$this->where($id);
$this->db->update($this->table, $data);
} elseif (is_numeric($id)) {
$filter = $this->filter;
$id = $filter($id);
$this->db->set($data);
$this->db->where($this->primary_key, $id);
$this->db->update($this->table);
} else {
$column_value = ( is_object($data) ) ? $data->{$id} : $data[ $id ];
$this->db->where($id, $column_value);
$this->db->update($this->table, $data);
}
} else {
if ($this->isMultiArray($data)) {
$this->db->insert_batch($this->table, $data);
$id = $this->db->affected_rows();
} else {
$this->db->insert($this->table, $data);
}
$id = $this->db->insert_id();
}
return $id;
}
本帖最后由 Aloghli 于 2016-8-26 10:44 编辑
//设置表单名
public function table($table){
//$this->db->from($table);
$this->table=$table;
return $this;
}//设置表单名
public function _table($table){
//$this->db->from($table);
$this->table=$table;
return $this;
}//删除;id可为数字,也可为数组;single位true只删除以后数据;
public function delete($id=NULL, $single = FALSE,$filed=NULL) {
$filter = $this->filter;
if (is_array($id)) {
if(isset($filed)){
$this->where_in($id,$filed);
}else{
$this->where($id);
}
} elseif (is_numeric($id)) {
$id = $filter($id);
$this->db->where($this->primary_key, $id);
}
if ($single) {
$this->limit(1);
}
$this->db->delete($this->table);
return $this->db->affected_rows();
}
//清空表单
public function clear() {
$this->db->empty_table($this->table);
return $this->db->affected_rows();
}//++某个字段值
public function upPlus($field, $id) {
$this->db->set($field, $field . '+1', FALSE);
if (is_array($id)) {
$this->where($id);
} elseif (is_numeric($id)) {
$filter = $this->filter;
$id = $filter($id);
$this->db->where($this->primary_key, $id);
}
$this->db->update($this->table);
return $this->db->affected_rows();
}
//--某个字段值
public function down($field, $id) {
$this->db->set($field, $field . '-1', FALSE);
if (is_array($id)) {
$this->where($id);
} elseif (is_numeric($id)) {
$filter = $this->filter;
$id = $filter($id);
$this->db->where($this->primary_key, $id);
}
$this->db->update($this->table);
return $this->db->affected_rows();
}
本帖最后由 Aloghli 于 2016-8-26 10:47 编辑
public function where($where = NULL, $value = NULL) {
if (isset( $where )) {
$this->_where = FALSE;
if (!is_array($where) && is_null($value)) {
$filter = $this->filter;
$this->db->where($this->primary_key, $filter($where));
} elseif (isset( $value )) {
$this->db->where($where, $value);
} elseif (is_array($where)) {
$this->db->where($where);
}
} else {
if ($this->_where)
$this->db->where($this->_where);
}
return $this;
}
public function or_where($where = NULL, $value = NULL) {
if (isset( $where )) {
$this->_where = FALSE;
if (!is_array($where) && is_null($value)) {
$filter = $this->filter;
$this->db->or_where($this->primary_key, $filter($where));
} elseif (isset( $value )) {
$this->db->or_where($where, $value);
} elseif (is_array($where)) {
$this->db->or_where($where);
}
}
return $this;
}
public function limit($limit = '', $offset = 0) {
if (!$limit) {
if ($this->_limit)
$this->db->limit($this->_limit);
} else {
$this->db->limit($limit, $offset);
}
return $this;
}
public function select($fields = NULL) {
if (isset( $fields )) {
$this->_fields = FALSE;
$fields = ( is_array($fields) ) ? implode(',', $fields) : $fields;
$this->db->select($fields);
} else {
if ($this->_fields) {
$_fields = ( is_array($this->_fields) ) ? implode(',', $this->_fields) : $this->_fields;
$this->db->select($_fields);
}
}
return $this;
}
public function order_by($order_by = '',$rand=false) {
if($rand){
$this->db->order_by($order_by, 'RANDOM');
}else{
if (!$order_by) {
if ($this->_order)
$this->db->order_by($this->_order);
} else {
$this->_order = '';
$this->db->order_by($order_by);
}
}
return $this;
}
public function where_in($fileds = '', $key = '') {
if ($fileds) {
if (!$key) {
$this->db->where_in($this->primary_key, $fileds);
} else {
$this->db->where_in($key, $fileds);
}
}
return $this;
}
public function like($value = '', $key = '', $match = 'both') {
if ($value) {
if (is_array($value) && !$key) {
$this->db->like($value);
} else {
if (!$key) {
$this->db->like($this->primary_key, $value, $match);
} else {
$this->db->like($key, $value, $match);
}
}
}
return $this;
}
public function group_by($group) {
$this->db->group_by($group);
return $this;
}
public function distinct($distinct) {
$this->db->distinct($distinct);
return $this;
}
本帖最后由 Aloghli 于 2016-8-26 10:49 编辑
public function count() {
return $this->db->get($this->table)->num_rows();
}
public function max($filed,$as = '') {
if ($as) {
return $this->db->select_max($filed,$as)->get($this->table)->row();
} else {
return $this->db->select_max($filed)->get($this->table)->row();
}
}
public function min($filed, $as = '', $object = TRUE) {
if ($as) {
if ($object) {
return $this->db->select_min($filed, $as)->get($this->table)->row();
} else {
return $this->db->select_min($filed, $as)->get($this->table)->row_array();
}
} else {
if ($object) {
return $this->db->select_min($filed)->get($this->table)->row();
} else {
return $this->db->select_min($filed)->get($this->table)->row_array();
}
}
}
public function avg($filed, $as = '', $object = TRUE) {
if ($as) {
if ($object) {
return $this->db->select_avg($filed, $as)->get($this->table)->row();
} else {
return $this->db->select_avg($filed, $as)->get($this->table)->row_array();
}
} else {
if ($object) {
return $this->db->select_avg($filed)->get($this->table)->row();
} else {
return $this->db->select_avg($filed)->get($this->table)->row_array();
}
}
}
public function sum($filed, $as = '', $object = TRUE) {
if ($as) {
if ($object) {
return $this->db->select_sum($filed, $as)->get($this->table)->row();
} else {
return $this->db->select_sum($filed, $as)->get($this->table)->row_array();
}
} else {
if ($object) {
return $this->db->select_sum($filed)->get($this->table)->row();
} else {
return $this->db->select_sum($filed)->get($this->table)->row_array();
}
}
} Hex 发表于 2016-8-26 10:06
赞。。。。。。但是,大概说说里面有啥吧
我不太善于表达,所以只能贴代码了,不好意思:$:handshake 感谢感谢
http://codeigniter.org.cn/forums/data/attachment/album/201608/26/162501trynriz0qijyii3r.png
这是我自己写的,你觉得怎么样,这种写法?帮忙看看呗谢谢了
这个确实没有解决什么问题 只是简单的封装了CI提供的CRUD而已
页:
[1]