开发者

Recommended Practice with Zend Framework's DB components?

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜