SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Threaded View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MVC (& frameworks) - how do you structure your Model class?

    For the past few months I've been working on and simultaneously using my own MVC framework. IMO, it's simple to use yet powerful (although which framework author wouldn't say the same thing? ), especially because of my Model class. Here's an example of how to create a new row in the Posts table:
    PHP Code:
    $post $this->loadModel('Post'); // Pass an ID as the 2nd param if you want to edit an existing row
    $post->setField('author''Steven');
    $post->setField('title''Some fake title');
    $post->setField('body''Lorem ipsum');
    $post->save(); 
    Every time setField() is called, an internal array (to the Model class) has a key => value pair added. The key is the name of the field (first param of setField()). Then when save() is called, the SQL statement is automatically generated.
    Naturally, if you setField() on a field that doesn't exist in the DB, the query would fail.

    Now today I was looking at the Recess framework (http://www.recessframework.org/) and noticed how he has his Model classes set up. You use an install tool that creates the Model classes automatically once you define the database structure (using the same tool). Within each class there would be properties for each field in the DB row. I'm trying to see if there's an advantage to having the fields hardcoded in the class, because I think I've seen other frameworks do the same thing. I imagine you'd still have to have a method similar to my setField() method, or you would just reference the property directly ($post->title = 'Some fake title'). Plus, if you ever update the DB table, you need to remember to go and edit the class to contain the new field as a property. A possible advantage to this method is that you could double check that you're only settings fields that exist by looking within the class before sending the SQL statement, but IMO that's a very small advantage.

    What are your thoughts on this? I'm certainly no expert, nor pretend to be one, but I really think Recess' method is overkill. Although if I'm wrong and there are advantages I'd love to hear them
    Last edited by Arrrms; Mar 25, 2009 at 18:43.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •