The class I will describe today is an extension of the Zend_Db_Table class that exposes a list of magic functions with the most commonly used operations over the database.
It uses the
__call function to add a functions to retrieve records from the database by simply calling a function like:
$this->getAllByProductIdAndStatus(1, 'Active'). When calling this function the following two field names are subtracted from the function name: “product_id” and “status”. Each of these fields are set to the parameters passed to the function. After that a query is built using these fields to restrict the result to only those records that have product_id set to 1 and status set to Active. The model defines the following function patterns:
const PATTERN_GET_BY='/getBy(.*)/';– returns only one record or null is none match the fields
const PATTERN_GET_ALL_BY='/getAllBy(.*)/';– returns all the matching records
const PATTERN_GET_ALL_LIKE='/getsAllLike(.*)/';– returns all the records that match the assigned SQL regular expressions
const PATTERN_GET_ALL='/getAllRows/';– returns all records from the current table
const PATTERN_FETCH_BY='/fetchBy(.*)/';– returns only one record
const PATTERN_FETCH_ALL_BY='/fetchAllBy(.*)/';– returns all the matching records
const PATTERN_FETCH_ALL_LIKE='/fetchAllLike(.*)/';– returns all the records that match the assigned SQL regular expressions
const PATTERN_FETCH_ALL='/fetchAllRows/';– return all records from the current table
const PATTERN_REMOVE_BY='/removeBy(.*)/';– removes all the matching records from the current table
const PATTERN_COUNT_BY='/countBy(.*)/';– counts the number of records by fields
const PATTERN_COUNT_LIKE='/countAllLike(.*)/';– counts the number of records using LIKE as a condition operator
const PATTERN_COUNT_ALL='/countAll/';– counts the number of records from the current table
As you can see there are 4 sets of functions:
- Functions that start with “get”. These functions return objects (default Zend_Db_Table_Row for single records and Zend_Db_Table_Rowset for multiple records).
- Functions that start with “fetch”. These function return arrays. Also they will join with all the tables set in the referenceMap property based on the name of the reference, like so <reference>__<referred_table_field_name>. An example would be the following: owner__first_name.
- Functions that start with “remove”. These functions delete the matching records from the current table.
- Functions that start with “count”. These functions return the number of matching records based on the given fields and their values.
Another useful function defined in the model is the “save” function. This function takes only one parameter of type array or object and it inserts the values in the database and returns the generated primary key. By default the “id” field is used as a primary key, but that can be changed by setting the
$primary property of the model. If the given array/object already contains the primary key field then the values are not inserted into the database, but rather the record with the matching primary key is updated. The “save” function always returns the value of the primary key field for the inserted or updated record.
In order to access the functions faster and now create objects each time I want to connect to the database I also created a static function named
this() that returns an instance of the current Ze_Model class. This way you can get the records from the database by simply calling something like:
You can use this class by adding it into your library/Ze/ folder and use it as the base for your new models instead of Zend_Db_Table class.
I have attached the full source code of the class to this post. I am looking forward to your thoughts and improvements.