【问题标题】:CodeIgniter Hooks for Active Record library用于 Active Record 库的 CodeIgniter Hooks
【发布时间】:2013-08-06 08:53:20
【问题描述】:

我需要一些帮助来理解 CodeIgniter 的钩子逻辑,以使代码适应我的需要。

页面:https://www.codeigniter.com/user_guide/general/hooks.html

事实上,我不得不从这里修改 MySQL 的数据库驱动程序:

function _from_tables($tables)
{
    if ( ! is_array($tables))
    {
        $tables = array($tables);
    }
return '('.implode(', ', $tables).')';
}

到这里:

function _from_tables($tables)
{
    if ( ! is_array($tables))
    {
        $tables = array($tables);
    }
return implode(', ', $tables);
}

我使用 Active Record 库制作了这个 mod 以使用 UNION 查询。

有人可以帮我做一个钩子,以防止我更新核心系统时我的修改被覆盖吗?

提前致谢!

【问题讨论】:

  • 我认为您正在寻找自定义库,而不是钩子。请参阅ellislab.com/codeigniter/user-guide/general/… -> 用您自己的版本替换库。您可以扩展 DB 驱动程序并用您自己的自定义版本替换一两个方法。

标签: php codeigniter hook


【解决方案1】:

您可以在 CodeIgniter Wiki - Extending Database Drivers 上找到扩展 db 驱动程序的说明

解决方案只需 3 个简单步骤:

1) 通过创建文件 MY_Loader.php 来扩展您的加载器类。把它 进入应用程序路径中的库目录(或者如果您是 使用 CI 2.x.x 然后将其放入 application\core\ 路径):

2) 将以下函数添加到您的 MY_Loader 类中:

3) 创建您自己命名的数据库驱动程序扩展类 MY_DB_mysql_driver.php(或用 mysql 部分代替 您使用的驱动程序 - 也为下面代码中的类名执行此操作!)。 将此文件也放在您的应用程序库目录中:

您的自定义数据库驱动程序将如下所示

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);
  }

}

【讨论】:

  • 我不知道我可以用助手修改 CI 函数!我做了一些我需要的功能,但不是核心 :) 谢谢!
  • 使用您的方法时出现以下错误:遇到 PHP 错误严重性:8192 消息:不推荐通过引用分配新的返回值文件名:core/MY_Loader.php 行号:32 什么我可以吗?
  • 删除$db =& new $my_driver(get_object_vars($db));中的&
  • 我现在收到致命错误:在第 201 行的 \system\libraries\Session.php 中调用未定义的方法 MY_DB_mysql_driver::where() 我将文件 MY_DB_mysql_driver.php 放在 applciation/libraries 和 MY_Loader applciation/core/ 中的 .php,对吗?
  • 你应该问另一个问题。评论区空间不足,无法调试问题
猜你喜欢
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 2013-06-14
  • 2016-02-25
  • 2013-04-11
  • 2014-05-23
  • 2013-02-18
  • 2011-07-10
相关资源
最近更新 更多