2

Newbie question about MVC structures here. So in an MVC, I currently have a page in my Views to display a list of results from a database query, in this bit of code I instantiate a class, run a method for sql query, and at last there's a for each loop and then displaying it with divs.

So my question is, if this bit of code is considered business logic and should be in a method in the Model, or is it part of the Views?

I hope you understand what I mean =) Thanks!

$listholder = new Categories_Model(); 
$data = $listholder->getCategories();

       $i = 1;
        foreach ($data as $row) {

            if ($i & 1) {
                echo '<div id="horizontalContainer" style="float: none; height: 50px";>';
                echo '<div id="listoverview1"><a href="'.URL.'categories/show/'.$row['id'].'">'.$row['catname'].'</a>';
                echo '</div>';
            } else {
                echo '<div id="listoverview1"><a href="'.URL.'categories/show/'.$row['id'].'">'.$row['catname'].'</a>';
                echo '</div></div>';
            }

            $i++;
       }
Mike Biff
  • 75
  • 1
  • 5

4 Answers4

1

No, that code is mainly view code. It only purpose is to allow an external process (a human) to see data in a form it likes. Assuming that the check for the first row is only important to the external process, not to your business logic

Model code shapes, gets, and sets data, some of which is persisted (database etc.) and some of which is calculated on demand

Some generalised, and simplified examples on things a model or view should do:

  • Model calculates the total of some financial figures
  • View flags each even numbered row so it can be rendered with a shaded background in the view

Also, in a classical MVC system, your first two lines of code would be in the controller, which would organise the dataset by using models and model methods to get data. The MVC system would then pass that data to the selected view to render

There are plenty of PHP based MVC frameworks like CAKE, KISS etc (not all are very good!). If you have a few dollars in your pocket, save yourself some time and stress, and load up Microsoft MVC

TFD
  • 22,085
  • 2
  • 30
  • 50
0

From your question you might have some understanding of MVC that has nothing to do with what other developer think what MVC is. That can happen, especially nowadays as some frameworks have used these three letters as a catchphrase without providing it actually.

From the code you outline in your question I'd say it is a typical Transaction Script and there is no specific kind of model nor view layer involved.

The transaction script transports business logic and you do not need to speculate a lot about view or models, just keep everything inside the transaction script.

Your transaction scripts will tend to become spaghetti code, however, with some little refactorings here and there over time you should be able to reduce duplicate code and I do not think your application will grow that large that it is actually not feasible any longer keep it maintained in transaction scripts.

So why being concerned about MVC if everything is in order with some other, well proven pattern?

hakre
  • 178,314
  • 47
  • 389
  • 754
  • Thanks for your reply! I haven't coded much for a few years now, and just recently took it up again, and I figured that learning MVC was a good idea. So I've followed a guide to create your own MVC as I thought that would be a good way to learn the basics. So now that I am continuing on this "project" and the guide was only for the very basics, I just wanted to be sure I was doing things the "correct" way so to speak. I'd rather do it right from the start then having to go back and re-learn something I missunderstood earlier. – Mike Biff Aug 06 '12 at 00:31
  • Apologies for the block of text, but I can't seem to be doing line breaks haha. – Mike Biff Aug 06 '12 at 00:35
  • 1
    hmm, let me think. probably this question and answer is interesting for you: [Understanding MVC](http://stackoverflow.com/q/10675512/367456); [How should a model be structured in MVC?](http://stackoverflow.com/q/5863870/367456) – hakre Aug 06 '12 at 01:29
0

Taking as an example Rails or CakePHP, your code doesn't follow the mvc pattern.

  • Model should only contain business logic (query/do something with fields of an object)
  • Controller should work "only" with receiving requests of webpage with params and send back the correct page
  • View Is basically an html page only

Obviusly this is a very short briefing of what html is.

In your case, the idea would be:

$listholder = new Categories_Model(); 
$data = $listholder->getCategories();

Which is a controller code (usually), getCategories is a Model method (ok as is). Then the controller will send some parameters to view which will know how to display those to the user, in this case your $data variable. Imagine something like printMyView($view_file_path, $data)

The view will be something like this: categories/show/">'; categories/show/">';

See http://php.net/manual/en/control-structures.alternative-syntax.php for alternative control structure syntax (quite nice). Maybe URLS are written better with sprintf or things like that (not how I did).

Basically you need 3 files (to split things logically), and the law is quite easy: don't write ever html in Controller, nor in Model.

Francesco Belladonna
  • 10,661
  • 12
  • 72
  • 141
0

The goal of MVC design pattern is to separate the presentation from the domain business logic. For that reason the business logic stays exclusively in model layer (model is a layer not any single class, if you have a class named Model, you are doing it wrong), whole the presentation layer contains views an controllers.

Controller instances are one that deal with user's requests, and change the state of model an view instances.

The view instances are the ones, that contain the presentation logic (just like domain objects contain the business logic in model layer). Views acquire information from model layer and decide, which templates will be used, or even whether template is even necessary. Views generate the response in your web site, and sending of redirect header is form of response too, which does not require any templates at all.

Also, you have to understand, that it is impossible to use classical MVC pattern with PHP (and highly complicated and impractical in the few web development, that would provide such option). Instead you would be using one of the MVC-inspired patterns.

These patterns are mostly distinguishable by how* view acquires information from model layer. The main options are: Model2 MVC, MVP, MVVM and HMVC.


In your case, the view should acquire the set of categories from model layer and then, if list is not empty and no exceptions were thrown, select a template which generates an unsorted HTML list.

The business logic would stay in the the model layer (in the domain object, which deals with categories), the presentation logic in the view and the process of turning it all in HTML - in the template, which view picked.

tereško
  • 56,151
  • 24
  • 92
  • 147