一、大纲
- 26、CI_DB_pdo_driver PDO数据库驱动类
-
- $this->db->affected_rows();
-
- $this->db->count_all("order_master");
-
- $this->db->count_all_results();
-
- $this->db->insert_id();
-
- $this->db->trans_enabled = true;
-
- $this->db->trans_begin();
-
- $this->db->trans_rollback();
-
- $this->db->trans_commit();
-
- $this->db->trans_status();
- 27、CI_DB_mysql_driver mysql数据库驱动类
-
- $this->db->affected_rows();
-
- $this->db->count_all("order_master");
-
- $this->db->count_all_results();
-
- $this->db->insert_id();
-
- $this->db->trans_enabled = true;
-
- $this->db->trans_begin();
-
- $this->db->trans_rollback();
-
- $this->db->trans_commit();
-
- $this->db->trans_status();
- 28、CI_DB_mysqli_driver mysqli数据库驱动类
-
- $this->db->affected_rows();
-
- $this->db->count_all("order_master");
-
- $this->db->count_all_results();
-
- $this->db->insert_id();
-
- $this->db->trans_enabled = true;
-
- $this->db->trans_begin();
-
- $this->db->trans_rollback();
-
- $this->db->trans_commit();
-
- $this->db->trans_status();
- 29、model模型类中引用其它model模型类(如:category_model)和数据库(如:product)
-
- public function __construct() {
- parent::__construct();
- $this->product_db = $this->load->database('product', true);
- $this->load->model('category_model');
- }
- 30、控制器中引用其它模型类(如:category_model)和数据库(如:product)
-
- public function __construct() {
- parent::__construct();
- $this->product_db = $this->load->database('product', true);
- $this->load->model('category_model');
- }
- 31、helper函数中引用CI超级对象的方法
-
- function get_order_status_by_order($order_status){
-
- $CI =& get_instance();
-
- $CI->load->Model('order_model');
-
- }
- 32、缓存驱动的加载方式
-
- $this->load->driver('cache', array('adapter' => 'memcached'));
-
- $this->load->driver('cache', array('adapter' => 'file'));
-
- $this->load->driver('cache', array('adapter' => 'redis'));
-
- $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
- 33、静态html模板文件中如何动态加载区域块内容
-
-
- <div include="/index.php/pub/common_nav" rel="include"></div>
-
-
- $(document).ready(function () {
- $('[rel=\'include\']').each(function (e) {
- var t = $(this),
- url = t.attr('include') + location.search;
- url && $.get(url, function (data) {
- t.html(data);
- })
- })
- })
- 34、拼接insert sql语句
-
- function add_user( $data ) {
- if ( empty($data) || !is_array($data) ) {
- return false;
- }
- foreach ($data as $key => $value) {
- if ( $value === '') {
- unset($data[$key]);
- }
- }
- $cols = array_keys($data);
- $values = array_values($data);
- $cols_str = implode(",", $cols);
- $values_str = "'".implode("','", $values)."'";
-
- $sql = "INSERT INTO user ({$cols_str}) VALUES ({$values_str})";
- $this->db->query($sql);
- }
- 35、拼接insert sql语句
-
- function add_user( $data ) {
- if ( empty($data) || !is_array($data) ) {
- return false;
- }
- foreach ($data as $key => $value) {
- if ( $value === '') {
- unset($data[$key]);
- }
- }
- $cols = array_keys($data);
- $values = array_values($data);
- foreach($values as $k=>$val){
- $values[$k]="'".$val."'";
- }
- $cols_str = implode(",", $cols);
- $values_str = implode(",", $values);
-
- $sql = "INSERT INTO user ({$cols_str}) VALUES ({$values_str})";
- $this->db->query($sql);
- }
- 36、拼接update sql语句
-
- function edit_user(userid, $data) {
- if ( empty($data) || !is_array($data) ) {
- return;
- }
- foreach ($data as $key => $value) {
- $str .= isset($str)?", {$key} = '{$value}'":"{$key} = '{$value}'";
- }
-
- $sql = "UPDATE user SET {$str} WHERE addr_id = '{$addr_id}'";
- $this->db->query($sql);
- }
- 37、数据库快捷操作类常用方法
-
- public function get_order_list($query, $offset = 0, $limit = 20) {
- if (is_array($query) && !empty($query)) {
- foreach ($query as $key => $val) {
- if (is_array($val)) {
- $this->order_db->where_in($key, $val);
- } else {
- $this->order_db->where($key, $val);
- }
- }
- }
- $this->order_db->order_by('updatetime', 'desc');
- $this->order_db->order_by('id', 'desc');
- if (!$limit) {
- $query = $this->order_db->get('order');
- } else {
- $query = $this->order_db->get('order', $limit, $offset);
- }
-
- if ($query->num_rows() > 0) {
-
- return $query->result_array();
- }
-
- return array();
- }
- 38、拼接select sql语句
- function get_user_list($cols=array("username","password")) {
- $col=implode(",",$cols);
- $sql = "SELECT $col FROM user ORDER BY addr_id DESC";
- $this->db->query($sql)->result_array();
- }
- 39、CI框架中cookie的三种使用方式
-
-
- setcookie("user_id",$user_info['user_id'],86500);
- setcookie("username",$user_info['username'],86500);
- setcookie("password",$user_info['password'],86500);
-
-
-
- $this->input->set_cookie("username",$user_info['username'],60);
- $this->input->set_cookie("password",$user_info['password'],60);
- $this->input->set_cookie("user_id",$user_info['user_id'],60);
-
-
-
-
-
-
- set_cookie("username",$user_info['username'],60);
- set_cookie("password",$user_info['password'],60);
- set_cookie("user_id",$user_info['user_id'],60);
-
- 40、array_merge()合并数组函数的使用
- <?php
- header("content-type:text/html;charset='utf-8'");
- $arr1=array(
-
- "13012"=>array(
- "brand_id"=>2,
- "category_id"=>3
- )
-
- );
-
- $arr2=array(
-
- "13012"=>array(
- "goods_id"=>3576,
- "goods_name"=>"sanyang"
- )
-
- );
-
-
-
-
- ?>
- 41.json格式数据:
- public function json(){
- $data[0]['goods_id']=3567;
- $data[0]['goods_name']="sanyang";
- $data[1]['goods_id']=3567;
- $data[1]['goods_name']="sanyang";
- echo json_encode($data);exit;
-
- }
- 42.联合查询 left join
- $query = array(
- 'product_id' => $product_id,
- 'activity.status' => array(1, 2, 0),
- 'activity.is_del' => 0
- );
- $query['activity.activity_id<>'] = $activity_id;
-
- $goods_list = $this->activity_model->get_activity_good_mapping($query, 0, 0);
-
- public function get_activity_good_mapping($query,$offset = 0, $limit = 0,$get=''){
- $this->db = $this->activity_db;
- if (is_array($query) && !empty($query)) {
- foreach ($query as $key => $val) {
- if (is_array($val)) {
- $this->db->where_in($key, $val);
- } else {
- $this->db->where($key, $val);
- }
- }
- }
-
- $this->db->from('activity_goods');
- $this->db->join('activity', 'activity_goods.activity_id = activity.activity_id','left');
-
- if (!$limit) {
-
- } else {
- $query = $this->db->limit($limit, $offset);
- }
- $query = $this->db->get();
- if ($query->num_rows() > 0) {
-
- return $query->result_array();
- }
-
- return array();
- }
- 43.ci框架如何记录sql日志?
-
- (1)配置日志目录: 在"application/config/config.php" 文件中配置日志目录
- $config['log_path']="/opt/www/logs/";
- (2)在 "system/database/DB_driver.php" 文件的query()方法末尾添加如下语句:
- log_message( 'db','【sql语句:'.$this->last_query().'】');
- return $RES;
二、部分截图如下:
三、数据库操作类之间的继承关系(区分数据库普通操作类和数据库快捷操作类),如图所示:
注意:CI_DB类是在DB()函数中动态定义的,没有单独的类库文件
DB()函数在DB.php文件中定义
DB.php文件中部分代码如下:
- require_once(BASEPATH.'database/DB_driver.php');
-
- if ( ! isset($active_record) OR $active_record == TRUE)
- {
- require_once(BASEPATH.'database/DB_active_rec.php');
-
- if ( ! class_exists('CI_DB'))
- {
- eval('class CI_DB extends CI_DB_active_record { }');
- }
- }
- else
- {
- if ( ! class_exists('CI_DB'))
- {
- eval('class CI_DB extends CI_DB_driver { }');
- }
- }
-
- require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');
-
- $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
- $DB = new $driver($params);
-
- if ($DB->autoinit == TRUE)
- {
- $DB->initialize();
- }
-
- if (isset($params['stricton']) && $params['stricton'] == TRUE)
- {
- $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"');
- }
-
- return $DB;
提示:$this->load->database();//内部通过加载DB.php文件并调用DB()函数来实现不同数据库驱动类的加载
$this->product_db = $this->load->database('product', true);
$this->wms_db = $this->load->database('wms', true);
$this->order_db = $this->load->database('order', true);
四、模型类分析:CI_Model
模型类分析:CI_Model
数据库普通操作类(CI_DB_driver)中的query()方法内部,通过$driver= $this->load_rdriver(); $RES= new $driver();两行代码加载并实例化了一个CI_DB_mysql_result结果集对象(总结:以驱动的方式加载结果集对象),经过对结果集对象的一些列初始化操作,最终返回此结果集对象(总结:query()方法中返回了一个CI_DB_mysql_result结果集对象)
- function load_rdriver(){
- $driver = 'CI_DB_'.$this->dbdriver.'_result';
-
- if ( ! class_exists($driver))
- {
- include_once(BASEPATH.'database/DB_result.php');
- include_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
- }
-
- return $driver;
- }
提示:也就是说,类似 $this->db->query("select * from user"); 的语句,返回的都是一个结果集对象
$this->db->query("select * from user")->result_array();//将结果集转换为二维关联数组
$this->db->query("select * from user")->row_array();//将结果集转换为一维关联数组
数据库快捷操作类(CI_DB_active_record)中的get()、insert()等多个方法内部都调用了数据库普通操作类中的query()方法,即:间接的返回了CI_DB_mysql_result结果集对象
数据库快捷操作类中很多对结果集的操作函数内部都是通过封装返回的CI_DB_mysql_result结果集对象中的方法来完成相似功能的(总结:get()、insert()方法内部最后调用了query()方法,并返回query()方法执行的结果,所以返回的也是一个CI_DB_mysql_result结果集对象)
数据库快捷操作类中的方法其实只干了一件事情,那就是通过函数的方式帮助我们拼接了要执行的sql语句,并执行该sql语句
但是对于复杂的sql语句,尤其对于复杂的多表查询语句,个人还是建议直接使用数据库普通操作类中的query()方法(适用于两个表以上的情况)
对于返回的结果集对象而言,结果集对象中的方法是通用的(总结:CI_DB_mysql_result结果集对象中对结果集的处理函数对于数据库普通操作类和数据库快捷操作类是通用的)
我们可以这样理解:数据库快捷操作类是对数据库普通操作类的二次封装
提示:数据库操作类的继承关系
class CI_DB_active_record extends CI_DB_driver {
//代码
}
http://blog.csdn.net/yanhui_wei/article/details/8883396