Zend Framework 的数据库组件的推荐做法?

发布于 2024-11-17 04:43:55 字数 1518 浏览 1 评论 0原文

首先是一些背景信息:

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满身野味 2024-11-24 04:43:55

我可以推荐的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文