Kohana 3.3数据库配置系统

时间:2022-10-13 16:11:38

I'm tried to set database config system. I attach new database config, load group and try get field value:

我试图设置数据库配置系统。我附加新的数据库配置,加载组并尝试获取字段值:

Kohana::$config->attach(new Config_Database);
$config = Kohana::$config->load('site');
$value = $config->get('title');
echo Debug::vars($value);

But i get only an error:

但我只得到一个错误:

ErrorException [ Notice ]: unserialize(): Error at offset 0 of 16 bytes MODPATH\database\classes\Kohana\Config\Database\Reader.php [ 64 ]

ErrorException [注意]:unserialize():错误0偏移量为16个字节MODPATH \ database \ classes \ Kohana \ Config \ Database \ Reader.php [64]

Config table structure:

配置表结构:

CREATE TABLE IF NOT EXISTS `config` (
  `group_name` varchar(128) NOT NULL DEFAULT '',
  `config_key` varchar(128) NOT NULL DEFAULT '',
  `config_value` text,
  PRIMARY KEY (`group_name`,`config_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `config` (`group_name`, `config_key`, `config_value`) VALUES
('site', 'description', 'Description'),
('site', 'title', 'Test title');

Tell me please, what wrong?

请告诉我,有什么不对?

1 个解决方案

#1


1  

This is because Config_Database expects the config values to be serialized. The error message is saying that the Reader was unable to unserialize the data you requested (because you seeded your database with non-serialized values). You should set the config values using:

这是因为Config_Database期望序列化配置值。错误消息表明Reader无法反序列化您请求的数据(因为您使用非序列化值为数据库播种)。您应该使用以下命令设置配置值:

$config->set('key', 'value')

For example:

例如:

Kohana::$config->attach(new Config_Database);
$config = Kohana::$config->load('site');       
$config->set('title', 'This is a title');

Now if we look at the data in the database you should see something like the following (take note of the format of the config_value field):

现在,如果我们查看数据库中的数据,您应该看到如下内容(请注意config_value字段的格式):

mysql> select * from config;
+------------+------------+-------------------------+
| group_name | config_key | config_value            |
+------------+------------+-------------------------+
| site       | title      | s:15:"This is a title"; |
+------------+------------+-------------------------+
1 row in set (0.00 sec)

#1


1  

This is because Config_Database expects the config values to be serialized. The error message is saying that the Reader was unable to unserialize the data you requested (because you seeded your database with non-serialized values). You should set the config values using:

这是因为Config_Database期望序列化配置值。错误消息表明Reader无法反序列化您请求的数据(因为您使用非序列化值为数据库播种)。您应该使用以下命令设置配置值:

$config->set('key', 'value')

For example:

例如:

Kohana::$config->attach(new Config_Database);
$config = Kohana::$config->load('site');       
$config->set('title', 'This is a title');

Now if we look at the data in the database you should see something like the following (take note of the format of the config_value field):

现在,如果我们查看数据库中的数据,您应该看到如下内容(请注意config_value字段的格式):

mysql> select * from config;
+------------+------------+-------------------------+
| group_name | config_key | config_value            |
+------------+------------+-------------------------+
| site       | title      | s:15:"This is a title"; |
+------------+------------+-------------------------+
1 row in set (0.00 sec)