<?php if ( ! defined(‘BASEPATH‘)) exit(‘No direct script access allowed‘);
/**
* 基模型
*/
class MY_Model extends CI_Model {
public $table = false;
public $primaryKey = ‘id‘;
public $_table = false;
public $_primaryKey = ‘id‘;
public function __construct() {
parent::__construct();
}
public function dbLists($select = ‘*‘, $param = array(), $tableKey = array(), $noDistinct = false) {
$return = array(‘total‘ => 0, ‘list‘ => array());
$this->setTableKey($tableKey);
$_param = array(‘total‘ => 1, ‘doPage‘ => 1);
$param = $param + $_param;
$page = (isset($param[‘page‘]) && $param[‘page‘] > 0) ? $param[‘page‘] : 1;
$limit = (isset($param[‘limit‘]) && $param[‘limit‘] > 0) ? $param[‘limit‘] : 20;
$offset = ($page - 1) * $limit;
$key = isset($param[‘key‘]) ? $param[‘key‘] : null;
$as = ‘‘;
$asTable = $this->_table;
if (isset($param[‘as‘])) {
$asTable = $param[‘as‘];
$as = ‘ as ‘ . $param[‘as‘];
}
$orderby = isset($param[‘order‘]) ? $param[‘order‘] : $asTable . ‘.‘ . $this->_primaryKey . ‘ DESC‘;
$where = isset($param[‘where‘]) ? $param[‘where‘] : array();
$like = isset($param[‘like‘]) ? $param[‘like‘] : array();
if (isset($param[‘total‘]) && $param[‘total‘]) {
$return[‘total‘] = $this->dbCounts($param ,$tableKey, $noDistinct);
}
$this->db->select($select, (strpos($select, ‘DISTINCT‘) === false) ? true : false);
if (isset($param[‘join‘])) {
foreach ($param[‘join‘] as $k => $v) {
$this->db->join($v[0], $v[1], isset($v[2]) ? $v[2] : ‘left‘);
}
}
if (isset($param[‘where_in‘])) {
foreach ($param[‘where_in‘] as $k => $v) {
$this->db->where_in($v[0], $v[1]);
}
}
$this->db->where($where);
$this->db->like($like);
$this->db->order_by($orderby);
if (isset($param[‘doPage‘]) && $param[‘doPage‘]) {
$this->db->limit($limit, $offset);
}
$return[‘list‘] = $this->db->get($this->_table . $as)->result_array();
if ($key) {
$result_by_key = array();
foreach ($return[‘list‘] as $k => $v) {
$result_by_key[$v[$key]] = $v;
}
$return[‘list‘] = $result_by_key;
}
return $return;
}
public function dbGet($select = ‘*‘, $id = ‘‘, $param = array(), $tableKey = array()) {
$this->setTableKey($tableKey);
$where = isset($param[‘where‘]) ? $param[‘where‘] : array();
$order = isset($param[‘order‘]) ? $param[‘order‘] : null;
if (!$id && !$where) {
return false;
}
$as = ‘‘;
$asTable = $this->_table;
if (isset($param[‘as‘])) {
$asTable = $param[‘as‘];
$as = ‘ as ‘ . $param[‘as‘];
}
$this->db->select($select, (strpos($select, ‘DISTINCT‘) === false) ? true : false);
$this->db->limit(1);
if (isset($param[‘join‘])) {
foreach ($param[‘join‘] as $k => $v) {
$this->db->join($v[0], $v[1], isset($v[2]) ? $v[2] : ‘left‘);
}
}
if (isset($param[‘where_in‘])) {
foreach ($param[‘where_in‘] as $k => $v) {
$this->db->where_in($v[0], $v[1]);
}
}
$id > 0 && $this->db->where($asTable . ‘.‘ . $this->_primaryKey, (int)$id);
$where && $this->db->where($where);
if ($order !== null) {
$this->db->order_by($order);
}
$data = $this->db->get($this->_table . $as)->row_array();
if ($data && $select != ‘*‘ && $select != $asTable . ‘.*‘ && !strstr($select, ‘,‘)) {
return $data[$select];
}
return $data;
}
public function dbAdd($data, $tableKey = array()) {
$this->setTableKey($tableKey);
$this->db->insert($this->_table, $data);
return $this->db->insert_id();
}
public function dbUpdate($data, $ids = ‘‘, $param = array(), $tableKey = array()) {
$this->setTableKey($tableKey);
$where = isset($param[‘where‘]) ? $param[‘where‘] : array();
$limit = isset($param[‘limit‘]) ? $param[‘limit‘] : null;
$order = isset($param[‘order‘]) ? $param[‘order‘] : null;
if (!$ids && !$where) {
return false;
}
if ($ids && !is_array($ids)) {
strval($ids);
$ids = explode(‘,‘, $ids);
}
$as = ‘‘;
$asTable = $this->_table;
if (isset($param[‘as‘])) {
$asTable = $param[‘as‘];
$as = ‘ as ‘ . $param[‘as‘];
}
is_array($ids) && count($ids) && $this->db->where_in($asTable . ‘.‘ . $this->_primaryKey, $ids);
if (isset($param[‘where_in‘])) {
foreach ($param[‘where_in‘] as $k => $v) {
$this->db->where_in($v[0], $v[1]);
}
}
$this->db->where($where);
if ($limit !== null) {
$this->db->limit($limit);
}
if ($order !== null) {
$this->db->order_by($order);
}
$updateTable = $this->_table . $as;
if (isset($param[‘join‘])) {
foreach ($param[‘join‘] as $k => $v) {
$updateTable .= ‘ ‘ . $v[2] . ‘ join ‘ . $v[0] . ‘ on ‘ . $v[1] . ‘ ‘;
}
}
return $this->db->update($updateTable, $data);
}
public function dbDel($ids = ‘‘, $param = array(), $tableKey = array()) {
$this->setTableKey($tableKey);
$where = isset($param[‘where‘]) ? $param[‘where‘] : array();
$limit = isset($param[‘limit‘]) ? $param[‘limit‘] : null;
$order = isset($param[‘order‘]) ? $param[‘order‘] : null;
if (!$ids && !$where) {
return false;
}
if ($ids && !is_array($ids)) {
strval($ids);
$ids = explode(‘,‘, $ids);
}
is_array($ids) && count($ids) && $this->db->where_in($this->_table . ‘.‘ . $this->_primaryKey, $ids);
$where && $this->db->where($where);
if (isset($param[‘where_in‘])) {
foreach ($param[‘where_in‘] as $k => $v) {
$this->db->where_in($v[0], $v[1]);
}
}
if ($limit !== null) {
$this->db->limit($limit);
}
if ($order !== null) {
$this->db->order_by($order);
}
return $this->db->delete($this->_table);
}
public function dbCounts($param = array(), $tableKey = array(), $noDistinct = false) {
$this->setTableKey($tableKey);
isset($param[‘where‘]) && $this->db->where($param[‘where‘]);
isset($param[‘like‘]) && $this->db->like($param[‘like‘]);
if (isset($param[‘join‘])) {
foreach ($param[‘join‘] as $k => $v) {
$this->db->join($v[0], $v[1], isset($v[2]) ? $v[2] : ‘left‘);
}
}
if (isset($param[‘where_in‘])) {
foreach ($param[‘where_in‘] as $k => $v) {
$this->db->where_in($v[0], $v[1]);
}
}
$as = ‘‘;
$asTable = $this->_table;
if (isset($param[‘as‘])) {
$asTable = $param[‘as‘];
$as = ‘ as ‘ . $param[‘as‘];
}
$this->db->select($noDistinct ? ‘‘ : ‘ DISTINCT ‘ . $asTable . ‘.‘ . $this->_primaryKey, false);
return $this->db->get($this->_table . $as)->num_rows();
}
public function setTableKey($tableKey = array()) {
if (key_exists(‘table‘, $tableKey)) {
$this->_table = $tableKey[‘table‘];
} else {
$this->_table = $this->table;
}
if (key_exists(‘key‘, $tableKey)) {
$this->_primaryKey = $tableKey[‘key‘];
} else {
$this->_primaryKey = $this->primaryKey;
}
}
}本文出自 “半城烟沙” 博客,请务必保留此出处http://vabc1314.blog.51cto.com/2164199/1381637
原文:http://vabc1314.blog.51cto.com/2164199/1381637