http://blog.csdn.net/yanhui_wei/article/details/25803945php
1、大綱html
- 2六、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();
- 2七、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();
- 2八、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();
- 2九、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');
- }
- 3一、helper函數中引用CI超級對象的方法
-
- function get_order_status_by_order($order_status){
-
- $CI =& get_instance();
-
- $CI->load->Model('order_model');
-
- }
- 3二、緩存驅動的加載方式
-
- $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'));
- 3三、靜態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);
- })
- })
- })
- 3四、拼接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);
- }
- 3五、拼接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);
- }
- 3六、拼接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);
- }
- 3七、數據庫快捷操做類經常使用方法
-
- 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();
- }
- 3八、拼接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();
- }
- 3九、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;
2、部分截圖以下:mysql
3、數據庫操做類之間的繼承關係(區分數據庫普通操做類和數據庫快捷操做類),如圖所示:jquery
注意:CI_DB類是在DB()函數中動態定義的,沒有單獨的類庫文件redis
DB()函數在DB.php文件中定義sql
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()函數來實現不一樣數據庫驅動類的加載json
$this->product_db = $this->load->database('product', true);windows
$this->wms_db = $this->load->database('wms', true);數組
$this->order_db = $this->load->database('order', true);
4、模型類分析: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 {
//代碼
}