Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Optimize Models in Zend Framework?

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.

like image 282
Jeremy Harris Avatar asked Feb 21 '12 15:02

Jeremy Harris


People also ask

What is a model in Zend Framework?

Models in Zend Framework. A Model defines the logical data representation of the application. For example, in a shopping cart application – Product, Customer, Cart and Orders are models. They define the properties of the entity it holds. Controllers communicate with models and ask them to retrieve information they need.

What is Zend Optimizer in PHP?

Zend is a framework of web application for PHP 5. This amazing framework gives you implementation of Model View Controller which has been designed for helping you in establishing a structure for your Zend applications. This article contains necessary and detailed information about Zend Optimizer you can get from a reliable web host.

How do I configure Zend Server to run a Zend Framework application?

To configure Zend Server to run a Zend Framework application: Create a new Zend Framework project. If you have not already done so, create a new Zend Framework project using the New Zend Framework Wizard in Zend Studio for Eclipse. Define a virtual host on Zend Server that will point to the new project's public directory:

How to use Zend Guard in PHP?

In order to use Zend Guard, you have to install Zend Optimizer in the web space before letting the code wok in a proper manner. Zend Optimizer is able to decode and then execute the files which are optimized with highly enhanced and improved productivity as compared with that of standard files of PHP.


2 Answers

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.

like image 61
RockyFord Avatar answered Oct 02 '22 02:10

RockyFord


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

like image 35
Tony Bogdanov Avatar answered Oct 02 '22 03:10

Tony Bogdanov