Welcome Guest, Not a member yet? Register   Sign In
Use My_Model or a separate model class from MY_Controller?
#1

[eluser]suntrop[/eluser]
HI there.

I am quite new to CI but have learned basic concepts. Now I am developing my first website with CI in its backbone.
Anyway, I'm wondering where to load my config variables from the database.

A) Create a config_model and load it in my MY_Controller (which is the parent of all my controllers).
B) Or create a MY_Model hierarchy like I did with my controllers and load the config vars from there.

Which one is better? Is there any problem with one of them – maybe I'll come across later?

Hope you can give some hints and help for a CI newbie :-)

Thanks a lot!

- suntrop -
#2

[eluser]mejlo[/eluser]
Hi,
what about "hooks"?
Create some function that loads config vars from database.
Then add to application/config/hooks.php something like:
Code:
$hook['pre_controller'] = array(
'class'    => '',
'function' => 'load_config_from_db', // function name
'filename' => 'config_db.php', // file where "load_config_from_db" is defined
'filepath' => 'hooks', // path to file above
'params'   => array()
);

$hook['pre_controller'] => function "load_config_from_db" will be called before your controllers are initialized
#3

[eluser]suntrop[/eluser]
Sounds good.

Had a look in the user guide but couldn't find how to access the variables? I need them in the controllers and views.
#4

[eluser]mejlo[/eluser]
Your hook:
Code:
<?php
function load_config_from_db()
{
  $_CI =& get_instance();
  $sql = 'SELECT key, val FROM ......';
  $query = $this->db->query($sql);

  while($query->result() as $row)
  {
    $_CI->config->set_item($row->key, $row->val);
  }
}
?>

then in your controller:
Code:
$view_data = Array();
$view_data['config_key'] = $this->config->item('config_key');
$this->load->view('view_name', $view_data);
#5

[eluser]wiredesignz[/eluser]
If you are using get_instance() in your code then the CI super object is available and you won't need to use a hook to access your database, it is just pointless over complication. Place your config loading code into your MY_Controller class.

Using a model will make your code more portable.
#6

[eluser]mejlo[/eluser]
ups :-)
in hook replace "$this->" with "$_CI->"
#7

[eluser]wiredesignz[/eluser]
@mejlo, Actually get_instance() does not work in a pre controller hook because there is no controller instance created yet.
#8

[eluser]mejlo[/eluser]
[quote author="wiredesignz" date="1297089206"]@mejlo, Actually get_instance() does not work in a pre controller hook because there is no controller instance created yet.[/quote]
Yes, it is not, but you create the instance....
Quote:$_CI =& get_instance();

I'm not sure if in 2.0 this works, but in 1.7.3 it works.
#9

[eluser]InsiteFX[/eluser]
In CI 2.0 the the pre hook is called before the CI instance is created!

InsiteFX
#10

[eluser]suntrop[/eluser]
Thanks to all.

Although it seems to be nice to hook my config before the controller I think the way wiredesignz described suits better to me.




Theme © iAndrew 2016 - Forum software by © MyBB