Stupidly Easy MVC - Group Membership Application - Part 2

This is part two of an example of using my Stupid Easy MVC framework in PHP. In this example we will talk about the how to use the Model. Originally I thought I might make the Directory a separate controller, but at this point I decided not to. I have some more ideas that I haven't quite worked out yet for extending this even to be more like the Rails framework for Ruby (again I do not expect to totally implement RoR, spare the language wars please!).

Here's the method in the Site Controller for calling the directory:

    function directory()
    {
        $directory_model = new Directory_Model();
        $members = $directory_model->get_all_active_members();
        $this->view->assign('members', $members);
        $this->view->display('directory');
    }

We create an instance of the directory model, call the method get_all_active_members and assign the result to a local variable, which is an array of all the active members. We assign it to the template and then call display. Easy Enough?

Lets look at the Directory Model now:

class Directory_Model extends MVC_Model {

    function get_all_active_members() {
        $sql = "SELECT * FROM members WHERE active=1 ORDER BY last, first";
        $result =  mysql_query($sql);
        $data = array();
        while ($row = mysql_fetch_array($result)) {
            $data[] = $row;
        }
        return $data;
    }

}

This is pretty self-explainatory. Select the data, loop though and put in array. If you were using PearDB you could then use the $db->getAll($sql) to eliminate the extra step of looping through the data.

I put the database connection in the config file and included it in index.php … if you used pearDB or some other db classes, you could instantiate it in the constructor for your model class (or the base class for that matter which right now is practically empty) then store the reference to the handler as a variable and use it something this: $this->db->query($sql)

Download app and library source (provided there are no more power outagages in Tampa where my server is, BTW looking for new hosting!)

The sql table commands are in a comment in the config file. Be sure and set the user/pass properly in the config. I called my database "Candy" ... when developing I display the name of the "action" at the top, so I can see whats going on. There's no CSS so this is a pretty sad looking little app!

I am going to try to put another element on the page, maybe the last 3 recent members so you can see how this would work with multiple elements. Do you have any other ideas?

Also I plan a review of Rasmus's article on his "No-framework MVC Framework" and on the MVC framework in the new Zend Framework Library.

Zend Framework

What did you think of the Zend Framework?

I'm working on it!!

Dang! it just came out! and you know about 10 of the 2 million other things I need to be doing! :) Actually did a bit today on it.

Hehe.. my reaction so far:

ahh...ehh... ok. hmm...

Going to attempt to write this simple app using the Zend Framework so we can see how they compare.

Taking software apart early

Taking software apart early and commenting on it is how we improve things :)