Tuesday, March 4, 2008

A model example

In a few hours I've created a trivia in CodeIgniter allowing send questions and answer some that other users sent.

The code is simple, but the interesting thing I want to show is the work of the models. In application/libraries/MY_Model.php the definition of the models is extended. Then we have the methods "get", "fetchlist", "count" receiving the type of object and an array of filters wanting to apply.

How are translated filters?
In general, the filters are conditions database. But it also allows "limit", "start", "orderby" and "orderbydir" which is to limit the amount of registration, change the start (offset), the column to sort and how (ASC/DESC) , respectively.
And even more, you can define filters themselves in each model. Defining a method "filter" can set own conditions. The method receives three arguments: object type, key and value. The function should return TRUE if they have a management own filter or FALSE if not. In the example, this can be seen in the model trivia.

What other methods have defined for the models?
Methods save, update, insert and delete receive an object and store it in the database. These objects should be objects that extend the Data_object class, be prefixed with "obj_" and must be called as the table it uses. Each must define the attributes that will be stored in columns in the database.

Download example