codeigniter-base-model 中文文档

codeigniter-base-model使用文档

1.ORM与其他查询的区别

//正常SQL
$result = $this->db->query("select * from table")->result_array();
//构造器
$result = $this->db->get($table)->result_array();
//ORM
$result = $this->model->get_all();

2.创建模型

models文件夹下创建Testa.php

<?php
class Testa extends MY_model{
    //如果数据库中不存在 表前缀+testa的表
    protected $_table = '你的表'; //存在表可以不写 默认表前缀+testa表
    public $_database = '切换其他库'; //可以不写  默认当前库
    protected $primary_key ='当前表查询primaryKEY'; //可以不写 默认表主键
    //下面开启 每个SQL都会拼接where 下面全部可以不写
    //where  deleted = 0;  //=1 修改$_temporary_only_deleted=true
    protected $soft_delete = true;
    protected $soft_delete_key = 'deleted';
    protected $_temporary_with_deleted = FALSE; //  0
    protected $_temporary_only_deleted = FALSE;  // 1
    
    //生命周期钩子 全部可以不写
    protected $before_create = array(); //数据创建之前
    protected $after_create = array();  //数据创建之后
    protected $before_update = array();  //数据更新之前
    protected $after_update = array();  //数据更新之后
    protected $before_get = array();  //数据获取之前
    protected $after_get = array(); //数据获取之后 存在值
    protected $before_delete = array(); //数据删除之前
    protected $after_delete = array(); //数据删除之后
    //比如 数据里面增加一个时间
    class Book_model extends MY_Model{
        public $before_create = array( 'timestamps' );
        protected function timestamps($book){
            $book['created_at'] = $book['updated_at'] = date('Y-m-d H:i:s');
            return $book;
        }
    }
    //更新/新增 数据 删除数组中的key
    public $protected_attributes = array('id');
    
}
​
//以上定义的方法和属性都可以不写只保留class

 

3.控制器调用

$this->load->model('Testa');
//获取表全部数据
$result = $this->Testa->get_all();

 

4.CRUD方法

where

_where($where) $where = ['id'=>1] 或者 $where = "id=1" 2种写法 下面$where通用

获取数据

get($id) 获取一条数据

get_by($where) 获取一条数据

get_many($where) 获取多条数据where in

get_many_by($where) 获取多条数据 where and

get_all() 获取表全部数据

dropdown($field) 获取字段的一个数组集合

插入数据

insert($data) 插入一条数据

insert_many($data) 插入多少数据

更新数据

update('10',$arr) 更新一条数据 $primary_key = 'id'; where id=10

update_many('11,12,13',$arr) 更新多条数据 where in(11,12,13)

update_by($where,$data)

update_all($data) 更新全部数据

删除数据

delete($id) 删除一条数据

delete_by($where) 删除满足条件数据 ['title'=>'1111']

delete_many($where) 删除主键数据 [1,2,3]

truncate() 清空表

统计数据

count_by($where) count(*) where

count_all() 统计全部数据

排序

order_by(['id'=>'desc','title'=>'desc'])

order_by('id','desc')

分页

limit($limit,$offset)

分组

group_by($field)

5.高级联表


class Testb_model extends MY_model{}
class Testd_model extends MY_model{}
class Testc_model extends MY_model{}
class Testa extends MY_model{
    //连接单条数据
    public $belongs_to = array(
        'Testb'=>['model'=>'Testb_model','primary_key'=>'s_id'],
        'Testc'=>['model'=>'Testc_model','primary_key'=>'ss_id'],
    );
    //连接多条数据
    public $has_many = [
        'Testd'=>['model'=>'Testd_model','primary_key'=>'c_id'],
    ];
    public  function  demo(){
        $result = $this
            ->with('Testb')
            ->with('Testc')
            ->get_all();
        dump($result);
    }

 



 

posted @ 2018-10-17 10:55  大智如蠢  阅读(469)  评论(0编辑  收藏  举报