My New Model Implementation |
[eluser]mdavis1982[/eluser]
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: Code: <?php 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: Code: <?php 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: Code: function _beforeSave() Please let me know what you all think as this is my first contribution to the community! Thanks! Matt
[eluser]mdavis1982[/eluser]
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! Thanks... Matt
[eluser]mdavis1982[/eluser]
Sorry about that... I've fixed it in the top post now! Matt |
Welcome Guest, Not a member yet? Register Sign In |