CodeIgniter Hooks for Active Record library

穿精又带淫゛_ 提交于 2019-12-11 02:38:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!