首页 > 解决方案 > Codeigniter: problem extending Database Driver

问题描述

Im using Codeigniter 2.x and been trying to extend the database class and followed the wiki but im getting this error:

Fatal error: Uncaught Error: Call to undefined method MY_DB_mysqli_driver::where() in /system/libraries/Session.php on line 218

Error: Call to undefined method MY_DB_mysqli_driver::where() in /system/libraries/Session.php on line 218

On line 218 of /system/libraries/Session.php:

$this->CI->db->where('session_id', $session['session_id']); 

My extended class is just the example from the wiki:

<?php
class MY_DB_mysqli_driver extends CI_DB_mysqli_driver
{
    public function __construct($params)
    {
        parent::__construct($params);
        log_message('debug', 'Extended DB driver class instantiated!');
    }

    public function get_first($table)
    {
        return $this->limit(1)->get($table);
    }
} 

标签: phpcodeignitercodeigniter-2

解决方案


你得到的错误实际上是正确的。我刚刚下载了第 2 版以确保它。

您正在扩展CI_DB_mysqli_driver您实际添加的内容应该在活动记录库中而不是数据库驱动程序中的时间。

而不是扩展您的CI_DB_driver创建扩展CI_DB_active_record并在其中添加get_first功能。

我必须补充一点,如果您想将这个功能添加到您的核心中,我什至不会费心用它来扩展核心,因为您只是在一行一行地交易,它没有任何好处。

此外,大多数这些扩展都可以在良好的基础模型中完成,而不是侵入核心。


推荐阅读