Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
【CodeIgniter3】クエリービルダー(旧アクティブレコード)を拡張する方法
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Loader extends CI_Loader
{
/**
* Database Loader
*
* @param mixed $params Database configuration options
* @param bool $return Whether to return the database object
* @param bool $query_builder Whether to enable Query Builder
* (overrides the configuration setting)
*
* @return object|bool Database object if $return is set to TRUE,
* FALSE on failure, CI_Loader instance in any other case
*/
public function database($params = '', $return = FALSE, $query_builder = NULL)
{
// Grab the super object
$CI = & get_instance();
// Do we even need to load the database class?
if ($return === FALSE && $query_builder === NULL && isset($CI->db) && is_object($CI->db) && !empty($CI->db->conn_id))
{
return FALSE;
}
require_once(BASEPATH . 'database/DB.php');
require_once(BASEPATH . 'database/DB_driver.php');
// ここから拡張クエリビルダークラスを読み込む処理を追加
if (!isset($query_builder) OR $query_builder === TRUE)
{
require_once(BASEPATH . 'database/DB_query_builder.php');
if (!class_exists('CI_DB', FALSE))
{
// 拡張したクエリビルダークラスを読み込む
$query_builder_path = APPPATH . 'libraries/database/MY_DB_query_builder.php';
if (file_exists($query_builder_path))
{
require_once($query_builder_path);
}
}
}
if ($return === TRUE)
{
return DB($params, $query_builder);
}
// Initialize the db variable. Needed to prevent
// reference errors with some configurations
$CI->db = '';
// Load the DB class
$CI->db = & DB($params, $query_builder);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment