Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Most people think of the "Fat Models, skinny controllers" paradigm, and it works better in the long run.</p> <p>A great rule of thumb is to think about your Models as their own entity, in the sense that if you were to move your models to a different framework for example, they would still be functional.</p> <p>A simple example would be saving information about an order on an e-commerce website. Let's say you want to save information about Tax, namely how much Tax is on the order. An abstract way of doing this is...</p> <p><code>$tax_amount = $order_amount * (TAX_PERCENTAGE / 100);</code></p> <p>Should we do this in the controller or the model? Suppose we did it in the controller, well... in our create action and our update action we would be calculating the tax, which makes for harder to maintain code and should the business rules of the e-commerce website change (say, we start selling to Tax exempt businesses or overseas business) then we would have to change any controller that saves order information.</p> <p>If we were to calculate Tax in our Order model however, we would be doing it once in our save() method, which would be called when editing and adding orders.</p> <pre><code>public function save() { //calculate tax first $q = $this-&gt;db-&gt;query($sql); } </code></pre> <p>Imo, it's better to enforce business rules in your model because it makes for more portable code and far less headaches when it comes to maintaining your code. Of course, there are people who will disagree, and this is a very subjective area. </p> <p>EDIT:</p> <p>Applying this to the specific question you asked, think about whether you would ever need the <code>login()</code> method anywhere else but your User model? It's possible you might want to split your models into different, more specific ones. But you could extend from your User model in that case. </p> <p>What about if you were to take away your controller completely? Or if you wanted to interface with your models in an entirely different way (say via a different framework in the future). Thinking in this way you would be far better off with your login method in your User model.</p> <p>Personally, I would be creating a login method on my model because it is an operation on data and that is what our models are for. I would also be creating a <code>loginAction()</code> method on my controller which would initiate the <code>login()</code> method on our model <strong>and</strong> perform any other action (for example, log failed attempts/redirect) that has to happen on post-login, should it be successful or unsuccessful. An example <code>loginAction()</code> may look as follows...</p> <pre><code>class UserController extends GenericController { public function loginAction() { $post = $this-&gt;form-&gt;getPost(); if(UserModel::login($post)) { //do something } else { //do something else } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload