CodeIgniter Forums
When to use models and helpers? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: When to use models and helpers? (/thread-25275.html)



When to use models and helpers? - El Forum - 12-06-2009

[eluser]ryan656[/eluser]
I have a helper file with a 'checkUser' function for login purposes - it takes a username and password, checks the database for a match and sets session information if they were found. Someone told me yesterday that I should not put any data queries inside helper functions. Should this function be in a model or should just the query call be in a model or what?

Does every function that performs CRUD on a database belong in a model? And is it the whole function or just the part that performs the query?

Thanks,
Ryan


When to use models and helpers? - El Forum - 12-06-2009

[eluser]Jamie Rumbelow[/eluser]
Ryan,

Helpers should only be used for small bits of repeated code or functionality that you use explicitly in your views. Any data related queries whatsoever should go in your models, and the login functionality itself should either go in a controller (if it's only being used once), or in a Library (if you're calling the same function more than once).

The MVC pattern might be a bit weird when you start out but it really makes sense when you've been using it for a while; and you'll be glad it's there!

Jamie


When to use models and helpers? - El Forum - 12-06-2009

[eluser]ryan656[/eluser]
Thank you for the reply. One more question, if I have a function that takes part of the subdomain in the url and queries the databse to see if theres a match, and i call this function from almost all of my controllers, where should this go? helper, model or library?

Thanks,
Ryan


When to use models and helpers? - El Forum - 12-06-2009

[eluser]Jamie Rumbelow[/eluser]
Model! It interacts with a database so you should put it in a model.

Jamie