Welcome Guest, Not a member yet? Register   Sign In
My New Model Implementation

Hi all...

I've recently been working with some large sites, and needed something to ease my development times. So, I created my Model implementation. It has been thoroughly tested on some large websites (upwards of 5000 requests per day), and hasn't fallen over once yet!

You can download it at: YDD_Model.php.zip

The class implements a simple Active Record pattern that automatically grabs field names from a DB table, supports validation, automatically uses created_at and updated_at fields if present in the DB table, and implements RoR-style dynamic finder methods. The
class also implements some filters that can be used such as _beforeValidation, _afterValidation, _beforeSave, _afterSave, _beforeUpdate and _afterUpdate.

To set it up, you put it into your application/libraries folder, and probably call it MY_Model.php. You will also need to change the class definition and constructor method.

Then, just create a model in your app like so:

    class News_Article extends YDD_Model
        function __construct()
        function initialise()
            // Validation rules for a news_article
            $validationRules['title']     = 'required|min_length[12]|max_length[255]';
            $validationRules['content']     = 'required|min_length[12]';
            $validationFields['title']             = 'News Article Title';
            $validationFields['description']     = 'News Article Description';


As soon as this class is instantiated, it will go off and look for a news_article table in your database. It assumes that database tables are singular, and that each has a column called id.

Once you've done this, you can then do funky stuff like:

    $news = $this->news_article->findByTitle('Some Title');

    // Or to fetch 3 news_article objects that have 'blah' in their title, ordered by their created_at date, use:
    $news = $this->news_article->searching('title', '%blah%')->ordering('created_at', 'ASC')->limiting(3)->findAll();

    // Or, to create a new news_article:
    $article = new News_Article();
    $article->title = 'My News Article Title';
    $article->content = 'My News Article Content';
    $article->save(); // This will fail if the object is not valid

    // Or, to update an article:
    $article = $this->news_article->find(2); // Find the news_article with id of 2
    $article->title = 'Edited Title';

I'm sure you get the idea. If you need to do anything before you model is saved, updated, validated, or after any one of those, you can simply declare the appropriate filter method in your inherited model class. For example, to set a unique slug for each news_article before it was saved, I could add this to the News_Article class:

function _beforeSave()
    // Before the object is saved we need to generate a unique slug for it
    $tempSlug = url_title($this->title);
    $articlesWithTheSameSlug = $this->filtering('slug', $tempSlug)->findAll();
    if (count($articlesWithTheSameSlug) > 0)
        $this->slug = $tempSlug . '-' . (count($articlesWithTheSameSlug) + 1);
        $this->slug = $tempSlug;

Please let me know what you all think as this is my first contribution to the community!



I also forget to mention, that if your database table has DATETIME columns called created_at, and updated_at, the class will automatically take care of those for you too!



Hi Matt, Your download link is broken.

Sorry about that...

I've fixed it in the top post now!


Theme © iAndrew 2016 - Forum software by © MyBB