Welcome Guest, Not a member yet? Register   Sign In
How to write and use static classes in CI?
#1

[eluser]Wades[/eluser]
Hi all,
I want to write a config class which will read the configuration from the db only once and can be read all throught the site, anybody who can give me some tips?
thanks a million!
#2

[eluser]davidbehler[/eluser]
You could extend the config library to not only load the configuration from file but from db aswell.
#3

[eluser]Phil Sturgeon[/eluser]
Extending Config class for DB work is a bad idea and won't work without some hacking. Remember how early the Config class is loaded compared to the DB drivers.

For PyroCMS I created a settings library that act's very much like config. It basically runs a query, stores the value in a property of the class then uses it next time (call it caching). If it is not in the class and not in the database then it checks in the $this->config->item();

Failing that too, it just returns FALSE. A fairly sturdy system, although I could do with changing it from query-per-item to your method of one query, store the lot.

Take a look at the code here. The model and library should be of interest to you.
#4

[eluser]Wades[/eluser]
Thanks Phil
btw, the PyroCMS is really cool.
I would like to participate in it. haha
#5

[eluser]davidbehler[/eluser]
You don't need that much hacking.
Just don't try to load the config from db as soon as the library is instantiated but create a pre_controller hook that calls a method of the library, e.g. $CI->config->load_from_db(); or something along that line.

Creating a new library instead of extending an existing one would be the next step, yes.
#6

[eluser]Phil Sturgeon[/eluser]
[quote author="waldmeister" date="1251122012"]You don't need that much hacking.
Just don't try to load the config from db as soon as the library is instantiated but create a pre_controller hook that calls a method of the library, e.g. $CI->config->load_from_db(); or something along that line.[/quote]

That makes sense, it just wasn't the impression I got from your post. The only issue with your suggestion is it requires an extra function call to load. Wades suggested he wanted it all loaded when the class is.

By making this into a new library you can autoload it and the db call code can be placed in the constructor to automate the process. Job done! :-)

[quote author="Wades" date="1251121872"]Thanks Phil
btw, the PyroCMS is really cool.
I would like to participate in it. haha[/quote]

PM me and let me know what you would like to do to improve PyroCMS. :-)




Theme © iAndrew 2016 - Forum software by © MyBB