Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Models in the Zend 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)

like image 479
rg88 Avatar asked Nov 17 '08 04:11

rg88


People also ask

Is Zend a MVC?

Zend\Mvc is a brand new MVC implementation designed from the ground up for Zend Framework 2, focusing on performance and flexibility. The MVC layer is built on top of the following components: Zend\ServiceManager - Zend Framework provides a set of default service definitions set up at Zend\Mvc\Service.

What is Zend used for?

Overview. Zend Framework is a collection of professional PHP packages with more than 570 million installations. It can be used to develop web applications and services using PHP 5.6+, and provides 100% object-oriented code using a broad spectrum of language features.

Is Zend a Framework?

Zend Framework is an open source, object oriented web application framework for PHP 5. Zend Framework is often called a 'component library', because it has many loosely coupled components that you can use more or less independently.


1 Answers

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.

like image 188
Bill Karwin Avatar answered Sep 23 '22 18:09

Bill Karwin