简体   繁体   English

Zend Framework ORM风格的表数据网关与扩展Zend_Db_Table_Abstract

[英]Zend Framework ORM-style table data gateway vs. extending Zend_Db_Table_Abstract

In the Zend Framework Quickstart , there has been a change from models that extend Zend_Db_Table_Abstract to the Table Data Gateway pattern. Zend Framework快速入门中 ,从将Zend_Db_Table_Abstract扩展到Table Data Gateway模式的模型发生了变化。

Personally, I have not had much experience with this pattern and I keep hearing this should most likely be used instead of the old way. 就个人而言,我对这种模式没有太多经验,我一直听说这应该最有可能被用来代替旧的方式。

A short example from the quickstart: 快速入门的简短示例:

Old way: 旧方式:

class Default_Model_Guestbook extends Zend_Db_Table_Abstract
{
    protected $_name = 'tablename';

    // do stuff
}

New way: 新方法:

// The actual model
class Default_Model_Guestbook
{
    protected $_comment;
    protected $_created;
    protected $_poster;
    // list continues with all columns
}

// Dbtable for this model
class Default_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract
{
    /** Table name */
    protected $_name    = 'guestbook';
}

// Mapper 
class Default_Model_GuestbookMapper
{
    public function save($model);
    public function find($id, $model);
    public function fetchAll();
}

From my lacking experience with this style of programming, I find it hard to grasp the actual benefits from this latter way; 由于我缺乏这种编程风格的经验,我发现很难从后一种方式中把握实际的好处; I understand that this method seperates the database from the actual logic as much as possible, which should in theory make a transition to another database platform easier. 我知道这种方法尽可能地将数据库从实际逻辑中分离出来,这在理论上应该更容易转换到另一个数据库平台。 However, I really don't see this happening on any project I am working. 但是,在我工作的任何项目中,我都没有看到这种情况发生。

There is almost no doubt that I am overlooking something, so I'd love to hear your advice. 毫无疑问,我忽视了一些事情,所以我很乐意听取你的意见。

The question: 问题:

  • Could someone please explain to me why (or if) the latter is better practice? 有人可以向我解释为什么(或者如果)后者是更好的做法?

  • Should I switch from the old way to the new way or are there still proper reasons for sticking with models that represent database tables? 我应该从旧的方式切换到新的方式还是仍然有适当的理由坚持代表数据库表的模型?

Thanks in advance. 提前致谢。

Here's my explanation at why this is a better practice: 以下是我为什么这是一个更好的做法的解释:

I think the real benefit to this is the ability to seamlessly change your data sources. 我认为真正的好处是能够无缝地更改您的数据源。 By adding an additional layer of abstraction into your application, your models no longer represent a database table (it never should have, in my opinion) as a model should be a representation of the data (not a gateway to it). 通过在应用程序中添加一个额外的抽象层,您的模型不再代表数据库表(在我看来它永远不应该),因为模型应该是数据的表示(不是它的网关)。 The database access layer should be encapsulated by the model, allowing you more flexibility. 数据库访问层应该由模型封装,从而为您提供更大的灵活性。

Say, for instance, your application needed to start using a SOAP service or XML-RPC as it's data source/storage. 例如,假设您的应用程序需要开始使用SOAP服务或XML-RPC作为其数据源/存储。 By using the data mapper approach, you are at a distinct advantage, as you already have the necessary structure in place to add these specific data layer interfaces without much (if any) interference with your existing models. 通过使用数据映射器方法,您具有明显的优势,因为您已经拥有必要的结构来添加这些特定的数据层接口,而不会对现有模型造成太大(如果有)干扰。

Should you do it, though? 你应该这样做吗? That's a pragmatic question. 这是一个务实的问题。 Personally, I like to have the peace of mind that I'm developing something that is flexible and follows (agreed on) best practices. 就个人而言,我喜欢安心,我正在开发一些灵活的东西并遵循(同意)最佳实践。 However, only you know whether creating a more flexible application will make your projects easier, either now or some time in the future, to build and maintain. 但是,只有您知道创建更灵活的应用程序是否会使您的项目在现在或将来的某个时间更容易构建和维护。

For me, I just like the feeling that I'm building something, I consider to be best-practice and it often pays dividends. 对我来说,我只是喜欢我正在构建一些东西的感觉,我认为这是最佳实践,它通常会带来好处。

It's useful because you can do $insert = new Model_Guestbook($param1, $param2, $param3); 这很有用,因为你可以做$insert = new Model_Guestbook($param1, $param2, $param3); - means when someone comes to the project, he can create a new instance easily without knowledge of the database structure (by checking the source / by model interface). - 意味着当有人进入项目时,他可以在不知道数据库结构的情况下轻松创建新实例(通过检查源/模型界面)。 This is just one of the benefits this method offers :) 这只是这种方法提供的好处之一:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM