duanji9481 2012-02-21 15:43
浏览 34
已采纳

如何在Zend框架中优化模型?

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 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:

    <?php
    
    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
    and
    Pádraic Brady at Surviving The Deepend

    for all their help in understanding this pattern.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的