Welcome Guest, Not a member yet? Register   Sign In
Extending CI_Config class problem
#1

[eluser]gigowski[/eluser]
I am writing my own shopping cart application, actually rewriting it for CI. I was attempting to create a config subclass as shown here. I followed the instructions in the user guide, Creating Your Own Libraries. I keep getting the following error:

Fatal error: Cannot redeclare class ci_config in /homepages/42/d187750613/htdocs/lilcharacters/system/libraries/Config.php on line 29

The name of my subclass is MY_Config and the filename is MY_Config.php and it's saved in the system\application\libraries folder. If I change the name of the file I get a message telling me it can't find the file. Does anyone know why this is happening? I call the MY_Config file up like so:

Code:
class Shop extends Controller {

        function Shop() {
             parent::Controller();
             $this->load->library('config');
             $this->config->db_config_fetch();
             $this->load->model('Shopmodel');
             $this->load->library('shop_lib');
        }
}

Here is the MY_Config.php code:

Code:
<?php
class MY_Config extends CI_Config {
    function MY_Config()
    {
        parent::CI_Config();
    }
    
    function db_config_fetch()
    {
        $CI =& get_instance();
        $query = $CI->db->get($this->item('dbt_site_config'));
        
        foreach ($query->result() as $row)
        {
            $this->set_item($row->config_key, $row->config_value);
        }
        
    }
    
}
?>

Any help would be greatly appreciated.
#2

[eluser]tonanbarbarian[/eluser]
Your problem is the following line in your controller
Code:
$this->load->library('config');
User Guide Config Class
The user guide shows that the config class is loaded automatically (the fairly prominent Note at the top of the page)
So trying to load the config library will result in the error you are getting because it is already loaded.
#3

[eluser]gigowski[/eluser]
It isn't the core config class that I am trying to load. I want to load my config class which extends the core config class. How can I do that? When I comment out the line:

Code:
//$this->load->library('config');
$this->config->db_config_fetch();

I get this error:

Fatal error: Call to undefined method: ci_config->db_config_fetch() in /homepages/42/d187750613/htdocs/lilcharacters/system/application/controllers/shop.php on line 8

I need it to load system/application/libraries/MY_Config.php but I can't figure out how.
#4

[eluser]tonanbarbarian[/eluser]
Hmm it should be working

I have successfully got it yo extend the Router class before and that is loaded automatically at the same time as the config (2 lines of code after) using exactly the same code
#5

[eluser]ejangi[/eluser]
@gigowski - which version of CI are you using?
#6

[eluser]Michael Wales[/eluser]
I'm thinking, since the native Config class is loaded by default - if you just create a file named MY_Config in /applications/libraries/ it would be loaded automatically.

Just like, if I created MY_Validation in /application/libraries/ it would be loaded automatically when I called $this->load->library('validation')
#7

[eluser]gigowski[/eluser]
[quote author="Michael Wales" date="1198151387"]I'm thinking, since the native Config class is loaded by default - if you just create a file named MY_Config in /applications/libraries/ it would be loaded automatically.

Just like, if I created MY_Validation in /application/libraries/ it would be loaded automatically when I called $this->load->library('validation')[/quote]

When I did that I received the following error:

Fatal error: Call to undefined method: ci_config->db_config_fetch() in /homepages/42/d187750613/htdocs/lilcharacters/system/application/controllers/shop.php on line 8

I'm just going to change the MY_Config class so that it doesn't need to be extended form the core config class. I was just using the example I found in the wiki for making a database config. If anyone can think of why this wouldn't work, I'm still interested.
#8

[eluser]Michael Wales[/eluser]
Quote:Fatal error: Call to undefined method: ci_config->db_config_fetch() in /homepages/42/d187750613/htdocs/lilcharacters/system/application/controllers/shop.php on line 8

Shouldn't it be:
Code:
$this->config->db_config_fetch()
#9

[eluser]esra[/eluser]
Try removing the constructor from MY_Config, then load the controller to see if the method is available.




Theme © iAndrew 2016 - Forum software by © MyBB