CodeIgniter Forums
Save Codeigniter config data in MySql database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Save Codeigniter config data in MySql database (/showthread.php?tid=70113)



Save Codeigniter config data in MySql database - SirTom - 02-22-2018

I am looking for a more elegant way to save Codeigniter config data in a MySql database.

This is my Way:

./application/hooks/load_config.php
PHP Code:
function load_config() {
 
   $CI =& get_instance();
 
   foreach($CI->siteconfig->get_config()->result() as $site_config) {
 
       $CI->config->set_item($site_config->key$site_config->value);
 
   }


./application/config/hooks.php
PHP Code:
$hook['post_controller_constructor'] = array(
            
'class'    => '',
            
'function' => 'load_config',
            
'filename' => 'my_config.php',
            
'filepath' => 'hooks'
        
); 

./application/config/config.php
PHP Code:
$config['enable_hooks'] = TRUE

./application/models/Siteconfig.php
PHP Code:
public function get_config() {
    return 
$this->db->get('config')->resulr_array();
}

public function 
set_config($key$value) {
    $config_data = array('key' => $key'value' => serialize($value));
    return $this->db->update('config'$config_data); 


The config-items are normaly stored in an config-array like this:
PHP Code:
$config["default"] = array('key' => 'value'); 

If I call get_config(), I get an array with another strukture:
PHP Code:
Array
(
   [
0] => Array(
                 [
key]   => ...,
                 [
value] => ...
               )


I am correcting this by using a foreach-loop:
PHP Code:
$result $this->siteconfig->get_config();
   foreach(
$result as $item => $value){
    
$this->config_vars[$value['key']] = $value['value'];


Is there a more elegant, performant way?

Thanks
Tom


RE: Save Codeigniter config data in MySql database - InsiteFX - 02-22-2018

Why not just encode and decode it using json then save it to the database in a text field.


RE: Save Codeigniter config data in MySql database - dave friend - 02-22-2018

I would drop the whole "hook" business and move it all to the model which would be autoloaded.

PHP Code:
class Siteconfig_m extends CI_Model
{
 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->get_config();
 
   }

 
   /**
     * Get the database items and add them to the super object $config item
     */
 
   public function get_config()
 
   {
 
       //using select() because it's lighter weight than get()
 
       //using result() because $config_items->item is less typing than $config_items['item'] 
 
       //and is usually easier to read. (Yes, it's a matter of opinion.)
 
       $config_items $this->db->query('select * from config')->result();  
        foreach
($config_items as $item)
 
       {
 
           //write directly to the "super object" $config array
 
           $this->config->config[$item->key] = unserialize($item->value); 
 
        }
 
   }

 
   public function set_config($key$value)
 
   {
 
       $config_data = array('key' => $key'value' => serialize($value));
 
       return $this->db->update('config'$config_data);
 
   }




RE: Save Codeigniter config data in MySql database - SirTom - 02-23-2018

I droped the whole "hook" business and move it to the sideconfig model which is autoloaded. Now I set the whole config in the constructor of the MY_Controller. Unfortunately, the hook "post_controller_constructor" was too late and the hook "post_controller" was too early. But now I found a solution.

I serialize/unserialize the data-array and save it as text in the Database.

Thanks for your help
Tom