Php – Models in the Zend Framework

modelmodel-view-controllerPHPzend-framework

What are some of the ways you have implemented models in the Zend Framework?

I have seen the basic class User extends Zend_Db_Table_Abstract and then putting calls to that in your controllers:

$foo = new User;

$foo->fetchAll()

but what about more sophisticated uses? The Quickstart section of the documentation offers such an example but I still feel like I'm not getting a "best use" example for models in Zend Framework. Any interesting implementations out there?


EDIT: I should clarify (in response to CMS's comment)… I know about doing more complicated selects. I was interested in overall approaches to the Model concept and concrete examples of how others have implemented them (basically, the stuff the manual leaves out and the stuff that basic how-to's gloss over)

Best Answer

I worked for Zend and did quite a bit of work on the Zend_Db_Table component.

Zend Framework doesn't give a lot of guidance on the concept of a "Model" with respect to the Domain Model pattern. There's no base class for a Model because the Model encapsulates some part of business logic specific to your application. I wrote a blog about this subject in more detail.

Persistence to a database should be an internal implementation detail of a Model. The Model typically uses one or more Table. It's a common but improper object-oriented design to consider a Model as an extension of a Table. In other words, we should say Model HAS-A Table -- not Model IS-A Table.

This is an example of IS-A:

class MyModel extends Zend_Db_Table_Abstract
{
} 

This is an example of HAS-A:

class MyModel // extends nothing
{
    protected $some_table;
}

In a real domain model, you would use $some_table in the methods of MyModel.

You can also read Martin Fowler's take on the Domain Model design pattern, and his description of the Anemic Domain Model antipattern, which is how many developers unfortunately approach OO programming.

Related Topic