Global mysql query |
[eluser]epseix[/eluser]
In an attempt to try and narrow my query down, I'm still very new to Codeigniter framework... I want to define global variables (eg. in autoloaded helpers) and use global mysql queries throughout my site - but I don't understand how to do the latter (global mysql queries). I understand the concept of defining single variables in a helper: Code: <?php ... and I understand the concept of creating a single mysql query in a model, loading it in a controller and using it in a view file (with a foreach loop): Model Code: <?php Controller Code: <?php View Code: <h2>Recommended Link</h2> How (and where) do I create a mysql query that can be autoloaded (or whatever) and used anywhere on my site - without the need to load it in every controller? Is there a simpler way to do all of the above?
[eluser]Ckirk[/eluser]
You can autoload the model, if that's what you're getting at. At the bottom of application/config/autoload.php you'll see the models option Code: $autoload['model'] = array('Default_model'); The model will be available to all controllers without having to load it then. Does that help?
[eluser]epseix[/eluser]
[quote author="Ckirk" date="1372251809"]You can autoload the model, if that's what you're getting at. At the bottom of application/config/autoload.php you'll see the models option Code: $autoload['model'] = array('Default_model'); The model will be available to all controllers without having to load it then. Does that help?[/quote] So, theoretically I can keep all global queries in a separate model and autoload it - allowing me to access it anywhere on my website? Perfect! One more thing - and I DID warn you I was a newbie - the view example will no longer work as the controller loads the query as $recommended_link, which isn't defined anymore - it would now be $this->... ?? in the foreach loop?
[eluser]Ckirk[/eluser]
It should still be defined because you still call the function in the model from the controller. Code: <?php correct?
[eluser]epseix[/eluser]
[quote author="Ckirk" date="1372255533"]It should still be defined because you still call the function in the model from the controller. Code: <?php correct?[/quote] Do I have to call the function in the controller? How would I use the autoloaded function in view without having to call every function in every controller?
[eluser]Syllean[/eluser]
Check out Phil Sturgeon's article CodeIgniter Base Classes: Keeping it DRY.
[eluser]Ckirk[/eluser]
You could indeed have a call to the function from the view but then you're breaking the principles of MVC. You should have all your business logic in that model and use the controller to call it. A viewer is exactly that... a view. Syllean linked a useful post by the legendary Phil Sturgeon. It's a worth while read I use a different method to autoload though, if you're following his post. Min goes like so: Code: if ( function_exists("__autoload") ) |
Welcome Guest, Not a member yet? Register Sign In |