Frage

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 ]

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?

War es hilfreich?

Lösung

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->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):

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)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top