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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行