CI繼承類MY_Model

其他子model類可繼承此類,例如:class User_model extends MY_Model
此繼承類需放在application/core下

子類繼承,即可使用父類中方法

<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
 * Class User_model 
 * @autor xx
 * @date  2019/12/10 16:13
 */
class User_model extends MY_Model
{
    public $_table = "user";
    public $_pk = "id";
}

MY_Model.php文件內容如下:

<?php
defined('BASEPATH') || exit('No direct script access allowed');

class MY_Model extends CI_Model
{

    public $_table = null;
    public $_pk    = null;

    public function __construct()
    {
        parent::__construct();
    }

    public function get_table()
    {
        return $this->_table;
    }
    public function get_pk()
    {
        return $this->_pk;
    }

    /**
     * 查詢 $pk 兩種方式 1.主鍵值  2列array('name'=> 'admin' )
     * @author xx
     * @date   2019-12-10T15:22:47
     * @param  [type]
     * @param  [type]
     * @return [type]
     */
    public function _detail($pk = null, $fields = null)
    {
        if (isset($fields))
        {
            $this->db->select($fields);
        }
        if (is_array($pk))
        {
            $this->db->where($pk);
        }
        else
        {
            $this->db->where($this->_pk, $pk);
        }
        return $this->db->get($this->_table)->row_array();
    }

    /**
     * 添加數據
     * @author xx
     * @date   2019-12-10T15:21:26
     * @param  [type]
     */
    public function _add($data)
    {
        $this->db->insert($this->_table, $data);

        return ($this->db->affected_rows() > 0) ? $this->db->insert_id() : false;
    }

    /**
     * $pk 兩種方式 1.主鍵值  2列array('name'=> 'admin' )
     * @author xx
     * @date   2019-12-10T15:21:20
     * @param  [type]
     * @param  array
     * @return [type]
     */
    public function _update($pk, $data = array())
    {
        if (is_array($pk))
        {
            $this->db->where($pk);
        }
        else
        {
            $this->db->where($this->_pk, $pk);
        }
        $this->db->update($this->_table, $data);

        return ($this->db->affected_rows() > 0) ? true : false;
    }

    /**
     * 刪除 $pk 兩種方式 1.主鍵值  2列array('name'=> 'admin' )
     * @author xx
     * @date   2019-12-10T15:18:45
     * @param  [type]
     * @return [type]
     */
    public function _delete($pk)
    {
        if (is_array($pk))
        {
            $this->db->delete($this->_table, $pk);
        }
        else
        {
            $this->db->delete($this->_table, array($this->_pk => $pk));
        }

        $this->db->delete($this->_table, array($type => $id));

        return ($this->db->affected_rows() > 0) ? true : false;
    }

    /**
     * 翻頁查詢構造器
     * @date   2019-12-10T09:39:46
     * @param  array  $data array('page'=>1,'limit'=>10)
     * @param  integer $count  查詢結果數量
     * @param  array $filter 查詢條件
     * @return array
     */
    public function get_datas($data = array(), &$count = 0, $filter = null)
    {
        $this->db->start_cache();

        if (null !== $filter)
        {
            //select,查詢字段,寫法:$select = "id,nickname,date";
            if (isset($filter['select']) && is_string($filter['select']))
            {
                $this->db->select($filter['select']);
            }
            // $filter['join'] = array(array('table'=>'auth_user','cond'=>'auth_user.role_id=role.id','type'=>'left'))
            if (isset($filter['join']) && is_array($filter['join']))
            {
                //_join寫法 array('table'=>'auth_user','cond'=>'auth_user.role_id=role.id','type'=>'left')
                //type 可選 left, right, outer, inner, left outer
                foreach ($filter['join'] as $_join)
                {
                    $this->db->join($_join['table'], $_join['cond'], $_join['type']);
                }
            }
            if (isset($filter['where']) && is_array($filter['where']))
            {
                //where,條件,寫法:$where = array('name =' => $name, 'id <' => $id);
                $this->db->where($filter['where']);
            }
            if (isset($filter['or_where']) && is_array($filter['or_where']))
            {
                $this->db->or_where($filter['or_where']);
            }
            if (isset($filter['where_in']) && is_array($filter['where_in']))
            {
                // where_in 條件 寫法  $where_in = array('id =' => array(1,2,3))
                foreach ($filter['where_in'] as $k => $v)
                {
                    if (is_array($v) && count($v) <= $this->max_in)
                    {
                        $this->db->where_in($k, $v);
                    }
                }
            }
            if (isset($filter['where_not_in']) && is_array($filter['where_not_in']))
            {
                // where_not_in 條件 寫法  $where_not_in = array('id =' => array(1,2,3))
                foreach ($filter['where_not_in'] as $k => $v)
                {
                    if (is_array($v) && count($v) <= $this->max_in)
                    {
                        $this->db->where_not_in($k, $v);
                    }
                }
            }
            //  $filter['like'] = array(array('filed'=>'name','match'=>'admin','side'=>'both'))
            if (isset($filter['like']) && is_array($filter['like']))
            {
                //like寫法 array('filed'=>'name','match'=>'admin','side'=>'both')
                // side 可選 before=>'%admin' after=>'admin%' both=>'%admin%'
                foreach ($filter['like'] as $_like)
                {
                    $this->db->like($_like['filed'], $_like['match'], $_like['side']);
                }
            }

            //order_by,可多個字段排序,寫法:id asc,create_time desc
            if (isset($filter['order_by']) && is_string($filter['order_by']))
            {
                $this->db->order_by($filter['order_by']);
            }
            //group_by,可多個字段排序,寫法:id,create_time
            if (isset($filter['group_by']) && is_string($filter['group_by']))
            {
                $this->db->group_by($filter['group_by']);
            }
        }
        $this->db->stop_cache();
        // $data 不爲空 分頁數據
        if ( ! empty($data) && isset($data['page']) && isset($data['limit']))
        {
            $offset = 0;
            if (1 != $data['page'])
            {
                $offset = bcmul(1, $data['limit']);
            }
            $items = $this->db->get($this->_table, (int) $data['limit'], (int) $offset)->result_array();
            $count = $this->db->count_all_results($this->_table);
        }
        else
        {
            // $data 爲空 取全部
            $items = $this->db->get($this->_table)->result_array();
            $count = count($items);
        }

        // $sqllog = $this->db->last_query();
        // log_message('debug', $sqllog);

        $this->db->flush_cache();

        return $items;

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章