I’m currently making an app in CodeIgniter and I was wondering on how I should deal with models. The way I have it set up at the moment:
Views: display
Controllers: display related things, form validation, redirects, talk to libraries, etc
Libraries: do the heavy lifting, talk to the models, generate error messages, logs, etc
Models: talk to the DB
(I’m currently using Active Record for most things)
Let me go straight to an example:
I have an Auth library and inside I have queries that area somewhat similar, varying only by the value in the WHERE clause.
Ex:
get_by_id($id);
get_by_password_hash($email);
get_by_email($email);
Should I have a bunch of these duplicate model functions or should I just have one simple function where I pass WHERE clauses and whatnot via the library?
Ex:
$where = array('id' => $id);
get($where);
Please let me know if you’d like me to clarify anything.
Thanks in advance.
If you have a lot of
get_by_*methods for a given model, then it does make sense to use the magic method as TaylorOtwell suggested. Actually, the__callmethod, as well as__getand__setare used frequently in other frameworks to make it easy to do exactly what you’re talking about. Just remember CI will not play nice with the __call Magic method on the controller while mapping (but that won’t effect the model).Basic way I look at CMV in CI:
Frequently, the controller will do things like:
As to passing where clauses — DB queries should almost be exclusively relegated to the model unless there is no other option or it would be excessively cumbersome to do so (but I would wonder if you couldn’t refactor that).