DBのラッパークラス
以前のエントリーでのDBラッパーですが、そのクラスだけ書いていたため、動かないぞというおしかりを受けましたw
次のように使います。
CI_Loaderクラスの
databaseメソッドを一部修正
/** * Database Loader * * @access public * @param string the DB credentials * @param bool whether to return the DB object * @param bool whether to enable active record (this allows us to override the config setting) * @return object KUNIHARU Tsujioka MYNETS_DB object */ function database($params = '', $return = FALSE, $active_record = FALSE) { // Grab the super object $CI =& get_instance(); //独自のラッパークラスを確認する。 //存在すればそれを開いて、ラップする仕組み。 // Do we even need to load the database class? if (class_exists('CI_DB') AND $return == FALSE AND $active_record == FALSE AND isset($CI->db) AND is_object($CI->db) ) { return FALSE; } //require_once(APPPATH.'libraries/DB'.EXT); require_once(BASEPATH.'database/DB'.EXT); if ($return === TRUE) { return DB($params, $active_record); } // Initialize the db variable. Needed to prevent // reference errors with some configurations $db = ''; // Load the DB class $db =& DB($params, $active_record); require_once APPPATH . 'libraries/MYNETS_DB'.EXT; $CI->db = new MYNETS_DB(&$db); //$CI->db = $db; // Assign the DB object to any existing models $this->_ci_assign_to_models(); }
まず、LoaderでDBを読み込む部分に手を入れます
通常
$db =& DB($params, $active_record);
でDB関数を$dbに入れて
$CI->dbパラメータに$dbとします。
これで
$this->db->queryが動くわけですが
ここで
require_once APPPATH . 'libraries/MYNETS_DB'.EXT;
$CI->db = new MYNETS_DB($db);
と、$dbを引数として渡すことで
MYNETS_DBクラスをインスタンス化し
$CI->db に渡します。
これでラッパークラスのMYNETS_DBを動かすことが可能となります。
MYNETS_DB
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /** * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * * @category * @package MYNETS_DB Class CI_DB wrapper * @author KUNIHARU Tsujioka <kunitsuji@gmail.com> * @copyright Copyright (c) 2008 KUNIHARU Tsujioka <kunitsuji@gmail.com> * @copyright Copyright (c) 2006-2008 Usagi Project (URL:http://usagi.mynets.jp) * @license New BSD License */ class MYNETS_DB { private $_db = NULL; public function __construct(&$db) { $this->_db = $db; } /** * データをすべて受け取る。結果をオブジェクトで受け取る * @param string sql * @param array bind parameter * @return mixd object or array() */ public function getAll($sql, $binds = FALSE) { if ($res = $this->query($sql, $binds)) { return $res->result(); } else { return array(); } } /** * データを指定件数受け取る。結果をオブジェクトで受け取る * @param string sql * @param int page番号 * @param int 1ページの件数 * @param array bind parameter * @return mixd object or array() */ public function getAllPage($sql, $page, $count, $binds = FALSE) { $offset = (intval($page) - 1) * intval($count); $limit = intval($count); $sql .= ' LIMIT ' . $limit . ' OFFSET ' . $offset . ' '; if ($res = $this->query($sql, $binds)) { return $res->result(); } else { return array(); } } /** * データを指定件数受け取るのと、トータル件数もつけて返す * @param string sql * @param int page番号 * @param int 1ページの件数 * @param array bind parameter * @return mixd object or array() */ public function getAllPagewithCount($sql, $page, $count, $binds = FALSE) { $offset = (intval($page) - 1) * intval($count); $limit = intval($count); $sql = str_replace('SELECT ', 'SELECT SQL_CALC_FOUND_ROWS ', $sql); $sql .= ' LIMIT ' . $limit . ' OFFSET ' . $offset . ' '; if ($res = $this->query($sql, $binds)) { return $res->result(); } else { return array(); } } /** * データをすべて受け取る。結果を配列で受け取る * @param string sql * @param array bind parameter * @return array result array */ public function getAllArray($sql, $binds = FALSE) { if ($res = $this->query($sql, $binds, FALSE)) { return $res->result_array(); } else { return array(); } } /** * データを1レコード受け取る。結果をオブジェクトで受け取る * @param string sql * @param array bind parameter * @return mixd object or array() */ public function getRow($sql, $binds = FALSE) { $sql .= ' LIMIT 1 OFFSET 0 '; if ($res = $this->query($sql, $binds)) { return $res->row(); } else { return array(); } } /** * データを1レコード受け取る。結果を配列で受け取る * @param string sql * @param array bind parameter * @return mixd object or array() */ public function getRowArray($sql, $binds = FALSE) { $sql .= ' LIMIT 1 OFFSET 0 '; if ($res = $this->query($sql, $binds, FALSE)) { return $res->row_array(); } else { return array(); } } /** * データを1カラム受け取る。 * @param string sql * @param array bind parameter * @return mixd */ public function getOne($sql, $binds = FALSE) { $sql .= ' LIMIT 1 OFFSET 0 '; if ($res = $this->query($sql, $binds, FALSE)) { return $res; } else { return FALSE; } } /** * INSERTする * @param string tablename * @param array insert data * @return mix (int)lastinsertid or FALSE * DBクラスのinsertメソッドをオーバーライドする * 成功した場合はIDを、失敗した場合はFALSEを返す */ public function insert($tablename, $option) { if ($this->_db->insert($tablename, $option)) { return $this->_db->insert_id(); } else { return FALSE; } } /* function query($sql, $binds = FALSE, $return_object = TRUE) { return $this->_db->query($sql, $binds, $return_object); } */ public function __call($func, $args) { if (method_exists($this->_db, $func)) { return call_user_func_array(array($this->_db, $func), $args); } else { show_error("method not exists."); } } public function __set($func, $args) { if (property_exists($this->_db, $func)) { $this->_db->$func = $args; } else { show_error("property not exists."); } } public function __get($func) { if (property_exists($this->_db, $func)) { return $this->_db->$func; } else { show_error("property not exists."); } } } ?>
おかしいところがありましたら、ぜひ突っ込みください。