Implements: Kohana_Config_Reader | Kohana_Config_Source | Kohana_Config_Writer
This class is a transparent base class for Config_Database_Writer and should not be accessed directly.
Database writer for the config system
Schema for configuration table:
CREATE TABLE IF NOT EXISTS config
(
group_name
varchar(128) NOT NULL,
config_key
varchar(128) NOT NULL,
config_value
text,
PRIMARY KEY (group_name
,config_key
)
) ENGINE=InnoDB;
Class declared in MODPATH/database/classes/Kohana/Config/Database/Writer.php on line 21.
$_db_instanceNULL
$_loaded_keysarray(0)
$_table_namestring(6) "config"
Tries to load the specificed configuration group
Returns false if group does not exist or an array if it does
string
$group
required - Configuration group boolean|array
public function load($group)
{
$config = parent::load($group);
if ($config !== false) {
$this->_loaded_keys[$group] = array_combine(array_keys($config), array_keys($config));
}
return $config;
}
Writes the passed config for $group
Returns chainable instance on success or throws Kohana_Config_Exception on failure
string
$group
required - The config group string
$key
required - The config key to write to array
$config
required - The configuration to write boolean
public function write($group, $key, $config)
{
$config = serialize($config);
// Check to see if we've loaded the config from the table already
if (isset($this->_loaded_keys[$group][$key])) {
$this->_update($group, $key, $config);
} else {
// Attempt to run an insert query
// This may fail if the config key already exists in the table
// and we don't know about it
try {
$this->_insert($group, $key, $config);
} catch (Database_Exception $e) {
// Attempt to run an update instead
$this->_update($group, $key, $config);
}
}
return true;
}
Constructs the database reader object
array
$config
= NULL - Configuration for the reader public function __construct(array $config = null)
{
if (isset($config['instance'])) {
$this->_db_instance = $config['instance'];
} elseif ($this->_db_instance === null) {
$this->_db_instance = Database::$default;
}
if (isset($config['table_name'])) {
$this->_table_name = $config['table_name'];
}
}
Insert the config values into the table
string
$group
required - The config group string
$key
required - The config key to write to array
$config
required - The serialized configuration to write boolean
protected function _insert($group, $key, $config)
{
DB::insert($this->_table_name, ['group_name', 'config_key', 'config_value'])
->values([$group, $key, $config])
->execute($this->_db_instance);
return $this;
}
Update the config values in the table
string
$group
required - The config group string
$key
required - The config key to write to array
$config
required - The serialized configuration to write boolean
protected function _update($group, $key, $config)
{
DB::update($this->_table_name)
->set(['config_value' => $config])
->where('group_name', '=', $group)
->where('config_key', '=', $key)
->execute($this->_db_instance);
return $this;
}