首页 > 其他 > 详细

扩展的CI model类

时间:2014-03-23 09:18:29      阅读:484      评论:0      收藏:0      [点我收藏+]
<?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

扩展的CI model类,布布扣,bubuko.com

扩展的CI model类

原文:http://vabc1314.blog.51cto.com/2164199/1381637

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!