Welcome Guest, Not a member yet? Register   Sign In
When to use models and helpers?
#1

[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
#2

[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
#3

[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
#4

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

Jamie




Theme © iAndrew 2016 - Forum software by © MyBB