Welcome Guest, Not a member yet? Register   Sign In
Best place to put AKA reading database from the config file..
#1

[eluser]Narkboy[/eluser]
I'm building a series of apps that work together to produce a series of e-commerce sites all drawing data from a common source.

Effectively we have:

EPOS Data Source <-----&gt; Config Site <------&gt; Public Sites (3 planned, more to come)

The Config Site is the only place that the web manager will be able to make changes, and these changes can be site-specific or cover all end sites.

The data in the cofig site must be manually updated from the EPOS data source - and this can take quite a lot of time (500k+ records checked and ammended or created as required). PITA, but unaviodable.

Amongst other things, the config site will have a master 'active' switch for each end site that will enable fast transition between the site being operational and displaying a maintenance page to permit updates.

The question is - how far up the CI process can I place this switch? I'd love to put it in the config file, so it's checked early, but I'm wary of putting a database call 1) inside the config file and 2) outside the standard method of accessing the db (using active record).

Suggestions / ideas / 'heres what I did's please!

Thanks!!
#2

[eluser]jedd[/eluser]
Hi Narkboy.

Can you please clarify ...

Quote:The question is - how far up the CI process can I place this switch? I'd love to put it in the config file, so it's checked early, but I'm wary of putting a database call 1) inside the config file and 2) outside the standard method of accessing the db (using active record).

Do you mean, within the scope of the public site (rather than the config) server , how would you, and where can you put a, call to check freshness of data on the config box?
#3

[eluser]Narkboy[/eluser]
Yeah - basically.

The Config Site will allow options to be set to the db which will govern each Public Site.

Really, what I'm wondering is - in each Public Site, how early in the CI process can I place a database call to confirm settings from the db?
#4

[eluser]jedd[/eluser]
[quote author="Narkboy" date="1239798997"]Yeah - basically.

The Config Site will allow options to be set to the db which will govern each Public Site.

Really, what I'm wondering is - in each Public Site, how early in the CI process can I place a database call to confirm settings from the db?[/quote]

Okay .. my preference is to [url="http://ellislab.com/codeigniter/user-guide/general/core_classes.html"]extend the core controller class[/url] - it's a bit of a panacea for many CI users.

Others suggest [url="http://ellislab.com/codeigniter/user-guide/general/hooks.html"]hooks[/url] , though I've yet to use them.

I think the question is better formulated as 'where in my logic do I need to know this answer', rather than where in the CI process. It's possible that the best place to determine what to do is outside of the public server box.
#5

[eluser]Narkboy[/eluser]
Brilliant thanks - it looks like hooks will be the easiest way to go. Sorry about the confused question - I'm on a steap learning curve!

Thanks!
#6

[eluser]CtheB[/eluser]
Hi hooks are things who needs to be loaded on a certain time in the initialisation proces.
Config files certainly doesn't need to be.

Having a quick look in the userguide:

Quote:If you would like to dynamically set a config item or change an existing one, you can so using:

$this->config->set_item('item_name', 'item_value');
Where item_name is the $config array index you want to change, and item_value is its value.

So in your main controller you can make a little method like this:
(in the constructer you load the model: $this->load->model('page_loader_model', 'loader', TRUE); )
Quote:
private function set_configuration()
{
$config = $this->loader->get_configuration();

foreach($config as $item)
{
$this->config->set_item($item->configurationKey, $item->configurationValue);
}
}

and then get the results from the model:
Quote:public function get_configuration()
{
$query = $this->db->query(" SELECT configurationKey,
configurationValue
FROM configuration
WHERE configurationActive IS TRUE
");
return $query->result();
}

And then your'r all set, you only have to call the set_configuration() method if necesarry.
#7

[eluser]Narkboy[/eluser]
Hi - thanks for your reply.

As I see it, the problem here is that 1) I need to place the set_configuration call in every controller and 2) the main CI libs, and all auto-load models, libs and helpers are already loaded before I get to the controller.

What I really need is to be able to grab the config variables from the db as early as possible to avoid wasted resources. If the system is in maintenance mode (set in the db) then I need to keep resource usage to an absolute minimum.

I've looked at hooks, but I think the best bet will to extend / replace the main Controller class so that it's constructor laods the config directly, thus avoiding having to call it from each of my controllers, and also ensuring that it's done as early as is feasible.

Thanks again!
#8

[eluser]CtheB[/eluser]
Excactly. That is what my post is saying.

In the route you set this:

$route['default_controller'] = "page_loader_controller";
$route['(.*)'] = "page_loader_controller/index/$1";

So it will load the main controller.

Then start the main controller class with this constructor:

Quote:class Page_loader_controller extends Controller {

public function __construct()
{
parent::Controller();
$this->load->model('page_loader_model', 'loader', TRUE);

$this->set_configuration();
}

So with my additional methods in the controller and the method you are able to do this everywhere in your application:

$this->config->item('some_value_from_your_configuration_table');
#9

[eluser]CtheB[/eluser]
P.S. if you don't want to load the items every time from the database you can set them in sessions. good luck.




Theme © iAndrew 2016 - Forum software by © MyBB