2012-02-21 15:43
浏览 34


I need to figure out a best practice for utilizing models efficiently in Zend Framework.

Currently, I have classes extending Zend_Db_Table_Abstract which handle my queries to each class' respective table.

When I need to access say 5 of those tables from a controller, I find myself creating 5 new instances of each specific Zend_Db_Table object. This is really ineffective.

I've thought about implementing a Factory pattern to create new instances (or provide existing static copy) but am not sure. Is this the best way to go about it?

What is the correct way to handle models ensuring speed without consuming excessive resources? Should lazy loading come into play here?

[EDIT] As an example, I have a class I use to handle getting details about a location from a raw search query and need these objects in order to parse the query:

// Initialize database object
$this->dbLocations = new Model_Locations;
$this->dbStates = new Model_States;
$this->dbZipcodes = new Model_Zipcodes;
$this->dbLookup = new Model_Lookup;

In another class, I may need to access those models again so I repeat the above code. Essentially reinitializing objects that could be static/singleton.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dslh32311
    dslh32311 2012-02-22 05:42

    I tend to work at the DbTable like you do. I've found it effective when I need to query multiple tables in a single action to create another layer of model above the dbTable. Similar to a service or domain layer. This way I only have to call a single model but I still have the functionality I need.

    Here is a simple example that may eventually interact with 5 DbTable classes and most likely a couple of Row classes as well:

    class Application_Model_TrackInfo
        protected $_track;
        protected $_bidLocation;
        protected $_weekend;
        protected $_shift;
        protected $_station;
        public function __construct() {
            //assign DbTable models to properties for convience
            $this->_track = new Application_Model_DbTable_Track();
         * @param type $trackId
         * @return type object
        public function getByTrackId($trackId) {
            $trackData = $this->_track->fetchRow($trackId);
            //getAllInfo() Application_Model_Row_TRack
            $result = $trackData->getAllInfo();
            //returns std object reflecting data from 3 DbTable classes
            return $result;
         *Get Station from trackid through bidlocationid
         * @param type $trackId
         * @return type object
        public function getStation($trackId){
            $data = $this->_track->fetchRow($trackId);
            //This a Application_Model_Row_Track method
            $result= $data->getStationFromBidLocation();
            return $result;

    I hope this helps.

    [EDIT] Since I wrote this answer I have learned the benefits of Domain Models and Data Mappers. Wow what a difference in my app. Not a magic bullet, but a huge improvement.
    Thanks to
    Alejandro Gervasio over at PHPMaster.com
    Rob Allen at Akrabat.com
    Pádraic Brady at Surviving The Deepend

    for all their help in understanding this pattern.

    点赞 评论
  • duanhui5344
    duanhui5344 2012-02-21 22:39

    You seem to be in a possition where you require efficient data management with features that the current Zend framework does not come with. Zend does not have a built in engine for working with databases of any sort, it simply has a wrapper classes which help you write your queries.

    What you need is an object relational model (ORM) which is a must-have in a professional framework. As I understand it, ORM is a framework by itself, it has patterns and strongly defined ways of "doing things", supports lazy loading (it takes the most of it) and optimizes your queries to the fullest. When you use ORM you don't even write SQL, instead you need to change your interpretation of data storage, you need to forget about tables and focus on Objects. In Doctrine for example each type (table) is specified by a Class and each record (row) as a class instance where you have access to different methods and properties. It supports event listeners and crazy cascading rellations.

    No more need to extract rows from related tables when you delete records (it is automatic), no more need to write complex and chaotic scripts to ensure filesystem synchronisation, you can migrate to almost any db engine at any time (mysql, postgresql, simplesql..) and more..

    I've been using Doctrine 2 in conjunction with Symfony 2 framework and I have to say I wouldn't go back to Zend for anything. Yes, it is complex and heavy, but really the ultimate solution. When you come to a moment when you need to manage hundreds of tables with millions of records total - then you will see the difference.

    So, final summation: ORM is what you need, there are many solutions, I know of two really good: Doctrine 1 or 2 and Propel.

    P.S.: ORM is an independant part of your system, so you don't really need to use a specific framework, Zend can be configured to work with Doctrine wonderfully :)

    点赞 评论