tags:

views:

198

answers:

2

Well I'm new to MVC and am trying to write a site in it. The site is a basic flash games site and has categories to track the games. My first step was to create a basic MVC setup and list all the categories. The problem is I don't know how to create an array with all of the categories in it. Can I add this to the controller? Should I be adding a new method to the model? Let me know, code is below.

index.php (controller)

<?php
    require_once 'database.php';
    require_once 'models/category.php';

    if (isset($_GET['page']))
    {
    }
    else
    { //display home page
        require_once 'views/home.php';
    }
?>

models/category.php (model)

<?php
    class Category
    {
        private $id,$name;

        function __construct($id)
        {
            $category_query = $database_connection->prepare('SELECT id,name FROM categories WHERE id=?');
            $category_query->execute(array($id));
            $category = $category_query->fetch();

            $this->id = $category['id'];
            $this->name = $category['name'];
        }

        function get_id()
        {
            return $this->id;
        }

        function get_name()
        {
            return $this->name;
        }
    }
?>

views/home.php (view)

List of all categories:
<ul>
<?php foreach ($all_categories as $category) { ?>
    <li><?php echo $category->get_name(); ?> (ID: <?php echo $category->get_id(); ?>)</li>
<?php } ?>
</ul>
+1  A: 

Very simply, you're just missing the glue to put it all together. This is often described as the "business logic". What you currently have as your "model" is actually your data storage layer. Your model needs to use this to return to the controller what it needs to give to the view.

Down in the code, you have several alternatives. One would be for the controller to use the data class you have to assemble the array the view wants to use. This is best if no other controller will need to do that. Another solution would be to add some model code to do that. This enables other controllers to call the same model to get the same data, but if you have other classes that need the same procedure, you would be repeating code. The third solution is to extend your data model to provide a collection view. Then the model code becomes a simple translation layer between what sort of collection the controller wants and asking the data model for it. It might even be simple enough to leave in the controller.

staticsan
+1  A: 

Like Staticsan said, there's lots of places you can put it, and books written about it. I'd change the constructor so that if you give it no arguments, it does no query. Then I'd use a static method that does its own query of all categories (or some subset) and then assembles an array of Category objects to return...

Probably better is to write a class called CategoryFactory that is just responsible for making and returning these objects. From there (or from the static method factory) you can make sure you're only doing one query.

MVC is not the end all be all for web application architecture, Fowler and Alur et al go into the shady details.

ehassler