Welcome Guest, Not a member yet? Register   Sign In
Change a config item permanently
#1

[eluser]vaxis[/eluser]
Hi guys,
what's the best way to change a config item permanently?
I tried the set_item but it set that item only for that block of code.

Another question: For basic application settings like app_name, app_version, company_name and so on is better using a config file or using a db table?

Thanks for attention.
#2

[eluser]Twisted1919[/eluser]
Save your config into database and use the items from there :
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Config extends CI_Config{

private $ci = NULL;

public function __construct()
    {
        parent::CI_Config();
    }

public function load_db_items()
    {
    if (is_null($this->ci)) { $this->ci =& get_instance(); }
        if (!$this->ci->db->table_exists($this->table))
        {
           $this->create_table();
        }
    $result = array();
    $query = $this->ci->db->get(TBL_CONFIG);
    if($query->num_rows() > 0)
    {
        $result = $query->result();
    }

        
    foreach ($result as $row)
        {
        $this->set_item($row->key, $row->value);
        }

    }

public function save_db_item($key, $value)
    {
        if (is_null($this->ci)) { $this->ci =& get_instance(); }

        $where = array('key' => $key);
        $found = $this->ci->db->get_where(TBL_CONFIG, $where, 1);

        if ($found->num_rows > 0)
        {
            return $this->ci->db->update(TBL_CONFIG, array('value' => $value), $where);
        }
        else
        {
            return $this->ci->db->insert(TBL_CONFIG, array('key' => $key, 'value' => $value));
        }            
    }

public function remove_db_item($key)
    {
        if (is_null($this->ci)) { $this->ci =& get_instance(); }
        return $this->ci->db->delete(TBL_CONFIG, array('key' => $key));
    }

public function create_table()
    {
        if (is_null($this->ci)) { $this->ci =& get_instance(); }
        $this->ci->load->dbforge();
        $this->ci->dbforge->add_field("`id` int(11) NOT NULL auto_increment");
        $this->ci->dbforge->add_field("`updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP");
        $this->ci->dbforge->add_field("`key` varchar(255) NOT NULL");
        $this->ci->dbforge->add_field("`value` text NOT NULL");
        $this->ci->dbforge->add_key('id', TRUE);
        $this->ci->dbforge->create_table(TBL_CONFIG, TRUE);
    }    
//        
}

/* End of file MY_Config.php */
#3

[eluser]erik.brannstrom[/eluser]
The config library works using arrays specified in the configuration files, meaning they are hard-coded. As such, they are not meant to be updated directly from your application. If you absolutely need to do this you'd have to create your own class that can write to these files yourself, however I wouldn't recommend it.

This sort of brings us to your second question. Any data that you expect to be changing dynamically (i.e. depending on user actions in the application) should be stored in a database. Other values that very rarely change (such as the name of a company) can be stored in a configuration file.

If you still have questions, I believe it would make it easier if you told us a bit more about what you want to be able to do. Anyway, I hope this helped!
#4

[eluser]InsiteFX[/eluser]
Code:
--
-- Table structure for table `settings`
--

CREATE TABLE IF NOT EXISTS `settings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `set_name` varchar(255) DEFAULT NULL,
  `set_data` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

InsiteFX
#5

[eluser]vaxis[/eluser]
[quote author="erik.brannstrom" date="1273269264"]The config library works using arrays specified in the configuration files, meaning they are hard-coded. As such, they are not meant to be updated directly from your application. If you absolutely need to do this you'd have to create your own class that can write to these files yourself, however I wouldn't recommend it.

This sort of brings us to your second question. Any data that you expect to be changing dynamically (i.e. depending on user actions in the application) should be stored in a database. Other values that very rarely change (such as the name of a company) can be stored in a configuration file.

If you still have questions, I believe it would make it easier if you told us a bit more about what you want to be able to do. Anyway, I hope this helped![/quote]

I made an application.php config file inside the config folder were are stored my application config info like app_version and stuff like this. How can i access this file and change the config[] array items values?

Thanks again for answers Smile
#6

[eluser]Genki1[/eluser]
Using CI 2.0.2, when I try to implement Twisted1919's code by calling the method load_db_items() from the __construct(), like this:

Code:
public function __construct()
{
  parent::__construct();
  $this->load_db_items();
}

I receive:

Quote:Fatal error: Class 'CI_Controller' not found in system/core/CodeIgniter.php on line 231

Is there any way to solve this? My goal is to set config variables at startup using values from a table so that they are available globally.
#7

[eluser]InsiteFX[/eluser]
Because you need to place this code in your ./application/config/config.php file
Code:
/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon. New Version!
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
| If using HMVC you do not need this! HMVC will autoload.
|
| Place this code at the bottom of your ./application/config/config.php file.
*/
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }
        elseif (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
        {
            include $file;
        }
    }
}
#8

[eluser]Genki1[/eluser]
I did as you suggested and still receive the same error.

When I add this code...

Code:
function __autoload($class = 'MY_Config')  // specified the name of my class file
{
    if (strpos($class, 'CI_') !== 0)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }
        elseif (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
        {
            include $file;
        }
    }
}
/* End of file config.php */
/* Location: ./application/config/config.php */

I still receive this error:

Quote:Fatal error: Class 'CI_Controller' not found in system/core/CodeIgniter.php on line 231

Any suggestions?
#9

[eluser]InsiteFX[/eluser]
1) You do not specify the name like that it searches the directory!

2) Did you save the controller as ./application/core/MY_Controller.php
#10

[eluser]Genki1[/eluser]
I really appreciate your help but so far I'm not having any success.

I have the following:

1. In ./application/config/config.php, I have added your code to the bottom of the file:

Code:
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        if (file_exists($file = APPPATH . 'core/' . $class . EXT))
        {
            include $file;
        }
        elseif (file_exists($file = APPPATH . 'libraries/' . $class . EXT))
        {
            include $file;
        }
    }
}

/* End of file config.php */
/* Location: ./application/config/config.php */

2. I have created ./application/core/MY_Controller.php with this code:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {  // have also tried: extends CI_Config

private $ci = NULL;

  public function __construct()
  {
      parent::__construct();
      $this->load_db_items();
  }

  public function load_db_items()
  {
      if (is_null($this->ci)) { $this->ci =& get_instance(); }
// have tried this with "...extends CI_Controller":
      $this->ci->config->set_item('TEST_ID', 'my_value');
// and have tried this with "...extends CI_Config":
      $this->set_item('TEST_ID', 'my_value');
  }

}

/* End of file application/core/MY_Controller.php */

3. I have created ./application/controllers/welcome.php with this code:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

  public function __construct()
  {
      parent::__construct();
  }  
  
  public function show_value()
  {
      echo 'TEST_ID =' . $this->config->item('TEST_ID');
  }

}

/* End of file application/welcome.php */

When I run index.php/welcome/show_value, I see:

Quote:TEST_ID =
(no value is displayed)

Can you see what I am doing incorrectly? I've spent a few hours at this and it's pretty frustrating.

Again, my goal is to set config variables at startup using values from a table so that they are available globally.




Theme © iAndrew 2016 - Forum software by © MyBB