问题
I need some help to understand CodeIgniter's hook logic to adapt the code to my needs.
The page : https://www.codeigniter.com/user_guide/general/hooks.html
In fact, I had to modify the database driver for MySQL from this :
function _from_tables($tables)
{
if ( ! is_array($tables))
{
$tables = array($tables);
}
return '('.implode(', ', $tables).')';
}
to this :
function _from_tables($tables)
{
if ( ! is_array($tables))
{
$tables = array($tables);
}
return implode(', ', $tables);
}
I made this mod to use UNION queries using Active Record library.
Can someone help me to make a hook in order to prevent my modification from being overwritten when I update the core system ?
Thanks in advance !
回答1:
You can find the instructions for extending the db drivers on the CodeIgniter Wiki - Extending Database Drivers
The solution comes in 3 simple steps:
1) Extend your loader class by creating the file MY_Loader.php. Put it into your libraries directory in the application path (or if you are using CI 2.x.x then put it into application\core\ path):
2) Add the following function to your MY_Loader class:
3) Create your Database driver extension class, that you name MY_DB_mysql_driver.php (or substitute the mysql part for whatever driver you use - do that also for the classnames in the code below!). Put this file also in your applications libraries directory:
Your custom DB driver will look like this
class MY_DB_mysql_driver extends CI_DB_mysql_driver {
function __construct($params){
parent::__construct($params);
log_message('debug', 'Extended DB driver class instantiated!');
}
function _from_tables($tables)
{
if ( ! is_array($tables))
{
$tables = array($tables);
}
return implode(', ', $tables);
}
}
来源:https://stackoverflow.com/questions/18061724/codeigniter-hooks-for-active-record-library