Zend Model Improvements

March 22nd, 2010 by Cosmin Harangus Leave a reply »

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:

  1. 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).
  2. 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.
  3. Functions that start with “remove”. These functions delete the matching records from the current table.
  4. 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: Model_Users::this()->getByUsernameAndPassword('john','doe');.

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.

Attached Files:

Share

2 comments

  1. Adrian says:

    Great article. Thanks for sharing this with us. As you stated, I have a suggestion: it will be useful to have a pattern for logical deletion, or an option for logical deletion for “removeBy”.

    Keep up the good work!
    Adrian

  2. Cosmin says:

    Logical deletion can be achieved using the save function and setting the primary key as an array with all the ids you want to update.

    PS: I just updated the Ze_Model class to allow users to remove using the LIKE condition.

Leave a Reply