Zend Framework 的数据库组件的推荐做法?
首先是一些背景信息:
我正在使用 Zend Framework 用 PHP 编写一个项目,并且我有一些来自过去与客户的会议的 E/R 图。数据库模式是为 MySQL 编写的,现在是时候在 Zend 中实现我的模型了。我对 Zend 相当陌生,所以我不知道如何开始很好地编写这段代码。
我在阅读他们的文档时注意到的第一件事是快速指南使用了数据网关模式,但他们的 Zend_DB 参考页面没有。
// 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);
}
在浏览 stackoverflow 一段时间后,希望找到如何最有效地组织模型的指导,我发现了 另一个不同的推荐解决方案。
现在上面链接的解决方案看起来非常干净,但在我开始编写代码之前,它仍然在我的脑海中留下了一个问题:
我应该使用 Row_Abstract 子类来存储数据模型,还是应该使用除了存储检索到的数据之外没有其他用途的单独模型?
后一个模型似乎需要大量重复工作(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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以推荐的 Zend DB 最佳实践是……不要使用它。这些组件非常基础,没有多大帮助。如果可以的话,改用 Doctrine,这样会好很多。
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.