CI繼承類MY_Model

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

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

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

class Admin_mdl extends MY_Model {

    const TBL_DB = 'admin_info';

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

}

MY_Model.php文件內(nèi)容如下:

/**
* MY_Model Class ,create by luguoli
*/
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model {

    var $TableName = '';

    public function __construct($TableName = '')
    {
        parent::__construct();
        $this->TableName = $TableName;
        log_message('debug', $this->TableName." Model Class Initialized");
    }
    
    public function get_data_by_id($id, $type='id')
    {
        $data = array();

        $this->db->select('*')->from($this->TableName)->where($type, $id)->limit(1);
        $query = $this->db->get();
        if($query->num_rows() == 1)
        {
            $data = $query->row_array();
        }
        $query->free_result();
        
        return $data;
    }


    public function get_datas($limit=NULL, $offset=NULL, $sort=NULL, $select=NULL, $where=NULL)
    {
        //limit
        if($limit && is_numeric($limit))
        {
            $this->db->limit(intval($limit));
        }
        
        //offset
        if($offset && is_numeric($offset))
        {
            $this->db->offset(intval($offset));
        }

        //sort,可多個(gè)字段排序,寫法:id asc,openid desc
        if($sort && !empty($sort))
        {
            $this->db->order_by($sort);
        }
        else
        {
            $this->db->order_by("id","desc");
        }

        //where,條件,寫法:$where = array('name =' => $name, 'id <' => $id);
        if($where && !empty($where))
        {
            $this->db->where($where);
        }

        //select,查詢字段,寫法:$select = "id,nickname,date";
        if($select && !empty($select))
        {
            $this->db->select($select);
        }

        return $this->db->get($this->TableName);
    }


    public function get_datas_count($where=NULL)
    {
        $data = array();

        if($where && !empty($where))
        {
            $this->db->where($where);
        }

        $this->db->select('count(*) as count')->from($this->TableName);
        $query = $this->db->get();
        if($query->num_rows() == 1)
        {
            $data = $query->row_array();
        }
        $query->free_result();

        return $data;
    }

    
    public function remove_data($id, $type='id')
    {
        $this->db->delete($this->TableName, array($type => $id));
        
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    
    public function add_data($data)
    {       
        $this->db->insert($this->TableName, $data);
        
        return ($this->db->affected_rows() > 0) ? $this->db->insert_id() : FALSE;
    }

    public function update_data($id, $data, $type='id')
    {

        $this->db->where($type, $id);
        $this->db->update($this->TableName, $data);
        
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

}

/* End of file MY_Model.php */
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,973評論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • 小編費(fèi)力收集:給你想要的面試集合 1.C++或Java中的異常處理機(jī)制的簡單原理和應(yīng)用。 當(dāng)JAVA程序違反了JA...
    八爺君閱讀 4,672評論 1 114
  • 寂寞的人數(shù)著花 浮笙 一朵梧桐花, 兩朵梧桐花, 三朵梧...
    Summer半笙閱讀 304評論 3 3
  • 昨天說的那位朋友告訴我她老公被人綁架了,說三天內(nèi)不還錢,后果自負(fù)。問我該怎么辦?我告訴她如果涉及人身安全就及時(shí)報(bào)警...
    柳月兒_閱讀 225評論 0 0