How to optimize models in Zend Framework?

I need to figure out how to use models effectively in the Zend Framework.

Currently, I have classes extending Zend_Db_Table_Abstract that process my queries in the corresponding table of each class.

When I need to get 5 such tables from the controller, I find that I am creating 5 new instances of each specific Zend_Db_Table object. It is really inefficient.

I was thinking of implementing a Factory pattern to create new instances (or provide an existing static copy), but I'm not sure. Is this the best way to do this?

What is the right way to handle speed models without consuming excessive resources? Do you need lazy loading here?

[EDIT] As an example, I have a class that I use to process location information from an unprocessed search query and need these objects 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 these models again in order to repeat the above code. Essentially, reinitializing objects that could be static / singleton.

+6
source share
2 answers

I usually work in DbTable, just like you. I found this efficient when I need to query several tables in one action to create another layer of the model over dbTable. Like a service or domain. Thus, I only need to call one model, but I still have the necessary functionality.

Here is a simple example that can end up interacting with the 5 DbTable classes, and most likely with several Row classes:

 <?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 learned about the benefits of domain models and data cards. Wow, what's the difference in my application. Not a magic bullet, but a huge improvement.
Thanks
Alejandro Gervasio over PHPMaster.com
Rob Allen on Akrabat.com
and
Pádraic Brady at Survival Depends

for their help in understanding this template.

+3
source

It sounds like you have the power when you need efficient data management with features that the current Zend environment doesn't work with. Zend does not have a built-in mechanism for working with databases of any type, it just has wrapper classes that help you write your queries.

You need a relational object model (ORM), which is mandatory in a professional environment. As far as I understand, ORM is a structure in itself, it has templates and clearly defined ways to "do something", supports lazy loading (it makes 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 the interpretation of the data warehouse, you need to forget about tables and focus on objects. In the Doctrine, for example, each type (table) is defined by the class and each record (row) as an instance of the class, where you have access to various methods and properties. It supports event listeners and crazy cascading relations.

You no longer need to extract rows from related tables when deleting records (this is automatic), you no longer need to write complex and chaotic scripts to ensure file system synchronization, you can transfer it to almost any db engine (mysql, postgresql, simplesql ..) at any time and much more..

I am using Doctrine 2 with the Symfony 2 framework, and I must say that I will not return to Zend for anything. Yes, it is a difficult and difficult, but really the final decision. When you come to the point where you need to manage hundreds of tables with millions of total records - then you will see the difference.

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

PS: ORM is an independent part of your system, so you really do not need to use a specific structure, Zend can be configured to work with Doctrine wonderfully :)

+1
source

Source: https://habr.com/ru/post/908991/


All Articles