Models in Zupal
Drupal uses the domain model -- that is, a data access responsibility class with the abilities of both an ActiveRecord object and a table class. The interface for a domain model is pretty straightforward:
Zupal_Domain_IDomain
- save() : void
- delete() : void
- find($crit = NULL, $sort = NULL) : Zupal_Domain_IDomain[]*
- findOne($crit = NULL, $sort = NULL) : Zupal_Domain_IDomain*
- findAll($sort = NULL) : Zupal_Domain_IDomain[]*
- toArray() : array
*A given implementor will return objects of the same type as itself.
The find methods are stripped down queries; they find all the records whose fields have the values specified by the key/value; i.e., to find all the people in a leads table of a given email address, pass array('email' => 'me@domain.com') to the find() method:
$stub = new Contacts_Model_Leads();
foreach($stub->find(array('email' => 'me@domain.com')) as $lead):
echo "<p>Lead ", $lead->id(),
' has email "me@domain.com"</p>';
endforeach;
While you can still access straight SQL calls through the embeddded table resource, I have found that 90% of the data I need can be achieved through the find interface -- an array of target property values and a sort key.
In most respects, the class acts like a Zend_Db_Table_Row_Abstract implementor -- you can get or set field values by name due to magic handling.
Zupal_Domain_Abstract implements Zupal_Domain and contains a table() method that returns a Zend_Db_Table. All the default domain classes use Zend_Db as a basis; however there is no reason that you can't create domain classes that use other ORMs or even low-level SQL or JSON techniques.
Zupal_Domain_Abstract
- Zupal Domain Abstract brings a lot more to the picture:
- Its constructor allows for a wide variety of input, from ID keys to pre-fetched Zend_Db_Table_Row objects to a special "stub" constant that prevents the object from representing any given row, turning it into a specialized table/factory class.
- It allows for generalized join definitions and "virtual field" access to joined rows.
- It contians a table() method that returns a Zupal_Table_Abstract class, itself a lightweight extension of Zend_Db_Table_Abstract.
- It has a find_from_sql() method that returns an array of Domain objects from a single Query, thus breaking the "One Query Per ActiveRecord" performance wall.
It also, of course, satisfies the above interface.

Post new comment