4

Here's a bit of context first:

I am writing a project in PHP using Zend Framework and I have some E/R diagrams from past meetings with the client. The database schema was written for MySQL and now it is time to implement my models in Zend. I am fairly new to Zend so I do not know how to start writing this code nicely.

The first thing I noticed while reading their documentation is that the quick guide used a data gateway pattern, but their Zend_DB reference page did not.

// Data Gateway Sample
// <application/models/UserMapper.php>
class Application_Model_UserMapper()
{
    // ... Methods to read/write from DB
}

// <application/models/DbTable/Users.php>
class Application_Model_DbTable_Users
{
    // Relationship infromation etc.
} 

// <application/models/User.php>
class Application_Model_User
{
    // At last, model specific data
    // using UserMapper->find($id, $model);
}

After browsing stackoverflow for a little while in hope of finding pointers in how to most efficiently organize the models, I came across yet another different recommended solution.

Now the solution linked above looks very clean, but it still leaves a single question in my mind before I set off to write my code:

Should I use Row_Abstract subclasses to store the data model, or should I make separate models that have no other purpose but store data retrieved?

The latter one seems like a lot of duplicate effort (Model_DbTable, Model, Mapper, [Row?])

// What I have in mind
 // <application/models/DbTable/Users.php>
class Application_Model_DbTable_Users
{
    // Relationship infromation etc.
}

// <application/models/User.php
class Application_Model_User extends Zend_Db_Table_Row_Abstract
{
    // Model stuff here
    // Relationship fetch helpers here
    // ...?
}

// Perhaps a Rowset_Abstract class if it is ever needed?
alxbl
  • 790
  • 4
  • 14

1 Answers1

0

The best practice I can recommand with Zend DB is ... don't use it. The components are very basic and do not help much. If you can, use Doctrine instead, which is a lot better.

Maxence
  • 12,868
  • 5
  • 57
  • 69
  • Thanks for the tip, I had been looking at Doctrine a bit, but it looks like it might take a while just to learn to use it. Do you have some resources you would recommend me to look at? (Besides their project page) – alxbl Jun 26 '11 at 10:06
  • No, the project documentation is the only resource I use. It is very good. – Maxence Jun 29 '11 at 09:30