【问题标题】:Kohana Database instances containts wrong dataKohana 数据库实例包含错误数据
【发布时间】:2013-02-08 16:10:18
【问题描述】:

我的 Kohana 数据库有问题。 有时我有错误

ErrorException [ Recoverable Error ] 
Argument 1 passed to Kohana_Database_Query_Builder_Select::compile() must be an instance of Database, string given, called in /srv/sites/mysite/www/modules/database/classes/kohana/database/query.php on line 230 and defined

这是因为在 Database:$instances 中包含字符串“dances”,但应该包含数组数据库配置。

这是我的配置:

<?php defined('SYSPATH') OR die('No direct access allowed.');

return array
(
'default' => array
(
    'type'       => 'MySQL',
    'connection' => array(
        'hostname'   => 'localhost',
        'database'   => 'chat',
        'username'   => 'root',
        'password'   => 'root',
        'persistent' => FALSE,
    ),
    'table_prefix' => '',
    'charset'      => 'utf8',
    'caching'      => FALSE,
    'profiling'    => TRUE
)
);

也许有人遇到过这样的问题或者可以帮助我吗?


对 DB 的任何查询都会导致错误。 像这样:

Jelly::factory('user', $user->id())

或者这个:

DB::select('value')->from('storage')->where('name', '=', 'salt')->limit(1)->execute();

或者这个:

ORM::factory('node')->where('type', '=', 'page-homepage')->find();

我不知道为什么会发生这个错误。我检查了所有方法都被调用了,我没有发现任何错误。

我通过类数据库中的写入方法instance解决了这个问题

public static function instance($name = NULL, array $config = NULL)
{
    if ($name === NULL)
    {
        // Use the default instance name
        $name = Database::$default;
    }

    if ( ! is_array(Database::$instances))
    {
        Database::$instances = array();
    }

    if ( ! isset(Database::$instances[$name]))
    {
        if ($config === NULL)
        {
            // Load the configuration for this database
            $config = Kohana::$config->load('database')->get($name);
        }

        if ( ! isset($config['type']))
        {
            throw new Kohana_Exception('Database type not defined in :name configuration',
                array(':name' => $name));
        }

        // Set the driver class name
        $driver = 'Database_'.ucfirst($config['type']);

        // Create the database connection instance
        new $driver($name, $config);
    }

    return Database::$instances[$name];
}

我添加条件如你所见

if ( ! is_array(Database::$instances))
{
    Database::$instances = array();
}

我不喜欢这样,但我别无选择。

【问题讨论】:

  • 你能不能在你得到错误的地方显示一段代码?

标签: database kohana instance


【解决方案1】:

我的疯狂猜测是,您似乎正在覆盖代码中 Kohana_Database 中的 compilequotequote_columnquote_identifierquote_tableexecute 方法,或者只是调用 @ 987654328@ 应该会触发此错误。

compile 方法不应该直接调用,它是一个内部函数,从execute() 调用

这些函数中的任何一个都将$db 作为第一个参数。不要传递任何东西,因为您想使用配置文件中设置的数据库,而不是尝试在查询构建器中手动设置它。

【讨论】:

  • 没有。此方法没有被覆盖。方法execute 不带参数调用。而且这个错误时有发生,没有规律。
猜你喜欢
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
相关资源
最近更新 更多