![]() |
Use My_Model or a separate model class from MY_Controller? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Use My_Model or a separate model class from MY_Controller? (/showthread.php?tid=38366) |
Use My_Model or a separate model class from MY_Controller? - El Forum - 02-06-2011 [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 - Use My_Model or a separate model class from MY_Controller? - El Forum - 02-06-2011 [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( $hook['pre_controller'] => function "load_config_from_db" will be called before your controllers are initialized Use My_Model or a separate model class from MY_Controller? - El Forum - 02-06-2011 [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. Use My_Model or a separate model class from MY_Controller? - El Forum - 02-07-2011 [eluser]mejlo[/eluser] Your hook: Code: <?php then in your controller: Code: $view_data = Array(); Use My_Model or a separate model class from MY_Controller? - El Forum - 02-07-2011 [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. Use My_Model or a separate model class from MY_Controller? - El Forum - 02-07-2011 [eluser]mejlo[/eluser] ups :-) in hook replace "$this->" with "$_CI->" Use My_Model or a separate model class from MY_Controller? - El Forum - 02-07-2011 [eluser]wiredesignz[/eluser] @mejlo, Actually get_instance() does not work in a pre controller hook because there is no controller instance created yet. Use My_Model or a separate model class from MY_Controller? - El Forum - 02-07-2011 [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. Use My_Model or a separate model class from MY_Controller? - El Forum - 02-07-2011 [eluser]InsiteFX[/eluser] In CI 2.0 the the pre hook is called before the CI instance is created! InsiteFX Use My_Model or a separate model class from MY_Controller? - El Forum - 02-07-2011 [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. |