CatalystModel
What is CatalystModel
CatalystModel is an extension to the CI Model. It provides some extra functionality not found in the default CI Model but found in other frameworks such as Django and Rails.
CatalystModel Functionality
Catalyst Model provides the following functionality over the current CI Model:
- Model to Database Table understanding
- Model->getCollection($set, $single): Returns an array of objects related to the current object
- $single is a boolean value to denote if getCollection should return a single object or an array of objects
- Model->getSet($set, $single): Calls getCollection
- Model->filter($single): Returns an array of the current objects based on the options
- Model->save(): Inserts|Updates a row in the database (determined automatically)
- Model->delete(): Deletes a row in the database
- Model->(CI ActiveRecord? DB Calls): These pass calls onto the CI ActiveRecord? DB calls. Used in conjunction with getCollection and filter
CatalystModel Conventions
Only a few conventions need to be followed:
- CatalystModel->$model_suffix: The $model_suffix needs to be defined in Catalystmodel.php. The default value is '_model'
- Model Classes:
- Class: Class<model_suffix>.
- Filename: class<model_suffix>.php
- Model->$dbitems instance variable: This array informs the CatalystModel->save() function which fields to save to db. Each class that extends CatalystModel needs this instance variable.
- Database Tables:
- <Class Name without $model_suffix>s in all lowercase. Example: Class: 'User_model' => Database Table: 'users'
CatalystModel Compatibility
My shared host is Dreamhost. CI does not work correctly using PHP4, thus I have only tested this using PHP5. Also, it has only been tested using CI 1.5.1 (therefore there is no init script).
I tried to make it PHP4 compatible, but gave up because I can't really test it and therefore can't say it will work :( I think there are some PHP5 class->PHP4 class converters online. I might try to use that in the future to provide a PHP4 version of CatalystModel. Unless someone wants to convert it for me, it'll be PHP5 only.
CatalystModel Download
Right now you can grab CatalystModel from SVN. It's one file. Put it in system/application/libraries.
CatalystModel Examples
class User_model extends CatalystModel {
var $first_name;
var $last_name;
var $age;
// This is how CatalystModel determines what to save to the DB.
var $dbitems = array('id', 'first_name', 'last_name', 'age');
}
class House_model extends CatalystModel {
var usersID;
var type;
var year_built;
// This is how CatalystModel determines what to save to the DB.
var $dbitems = array('id', 'usersID', 'type', 'year_built');
}
$obj = new User_model(); $obj->first_name = 'Terry'; $obj->last_name = 'Brooks'; $obj->age = 32; $obj->save(); // There should now be a new row in the database containing the above information $obj->age = 68; $obj->save(); // The row in the database has now been updated with new information $obj->delete(); // The row in the database has now been deleted
$obj = new User_model();
$users = $obj->where('id', 1)->filter(true);
// $users is a User_model object that represents the data in the dbrow.
$users = $obj->where('id', 1)->orwhere('id', 2)->filter();
// $users is now an array of size 2 containing User_model objects that represents the data in the dbrow.
$users = $obj->where('id', 10)->filter();
// $users is now an empty array
$users = $obj->where('id', 10)->filter(true);
// $users is now null
$obj = new User_model();
$user = $obj->where('id', 1)->filter(true);
$houses = $user->getSet('houses');
// $houses now contains an array of House_models that represent the following data from the database
// 1, 1, Mansion, 1983
// 1, 1, Condo, 2004
// 1, 1, Cardboard Box, 2006
// We can also specify options for getSet, identical syntax to filter();
$houses = $user->where('year_built >', 2000)->getSet('houses');
// $houses now contains an array of House_models that represent the following data from the database
// 1, 1, Condo, 2004
// 1, 1, Cardboard Box, 2006
// You can also go backward for 1:1 relationships.
$HouseModel = new House_model();
$house = $HouseModel->where('id', 1)->filter(true);
$user = $house->getCollection('users', true);
// $user should be the User_model object of the House_model in question.
