简体   繁体   English

在Zend中,为什么我们使用DB Model类和Mapper类作为两个独立的?

[英]in Zend, Why do We use DB Model class and Mapper class as two separate?

I am working on the zend project, I am referring on other zend project to create the new Zend Project.But I don't like to blindly follow that project without understanding. 我正在研究zend项目,我指的是其他zend项目来创建新的Zend Project.But我不喜欢盲目地遵循该项目而不理解。 In the Zend Directory structure, In Model class there are mainly two type of classes I see, like as in 在Zend目录结构中,在Model类中,我看到的类主要有两种类型,如同

- models
   - DbTables
        - Blog.php  //Extends Zend_Db_Table_Abstract
   - Blog.php       // Contains methods like validate() and save()
   - BlogMapper.php // Also Contains methods like validate(Blog b) & save(Blog b)

Why this specific structure is followed? 为什么遵循这种特定的结构? Is this is to separate Object class and Database model class? 这是分开Object类和Database模型类吗?

Please explain. 请解释。

DataMapper is a design pattern from Patterns of Enterprise Application Architecture . DataMapper是来自企业应用程序架构模式的设计模式

The Data Mapper is a layer of software that separates the in-memory objects from the database. Data Mapper是一个软件层,用于将内存中对象与数据库分开。 Its responsibility is to transfer data between the two and also to isolate them from each other. 它的职责是在两者之间传输数据,并将它们彼此隔离。 With Data Mapper the in-memory objects needn't know even that there's a database present; 使用Data Mapper,即使存在数据库,内存中的对象也不需要知道; they need no SQL interface code, and certainly no knowledge of the database schema. 他们不需要SQL接口代码,当然也不了解数据库模式。

How you store data in a relational database is usually different from how you would structure objects in memory. 如何在关系数据库中存储数据通常与在内存中构造对象的方式不同。 For instance, an object will have an array with other objects, while in a database, your table will have a foreign key to another table instead. 例如,一个对象将有一个包含其他对象的数组,而在数据库中,您的表将具有另一个表的外键。 Because of the object-relational impedance mismatch , you use a mediating layer between the domain object and the database. 由于对象 - 关系阻抗不匹配 ,您在域对象和数据库之间使用中介层。 This way, you can evolve both without affecting the other. 这样,您可以在不影响另一方的情况下进化。

Separating the Mapping responsibility in its own layer is also more closely following the Single Responsibility Principle . 单一责任原则中,更紧密地遵循其自身层中的映射责任。 Your objects dont need to know about the DB logic and vice versa. 您的对象不需要了解DB逻辑,反之亦然。 This gives you greater flexibility when writing your code. 这为您在编写代码时提供了更大的灵活性。

When you dont want to use a Domain Model, you usually dont need DataMapper. 当您不想使用域模型时,通常不需要DataMapper。 If your database tables are simple, you might be better off with a TableModule and TableDataGateway or even just ActiveRecord. 如果您的数据库表很简单,那么使用TableModule和TableDataGateway甚至只是ActiveRecord可能会更好。

For various other patterns see my answer to 对于其他各种模式,请参阅我的答案

The idea of a Model is to wrap up the logical collection of data inside of your code. 模型的概念是在代码中包含逻辑数据集合。

The idea of a DataMapper is to relate this application-level collection of data with how you are storing it. DataMapper的想法是将这个应用程序级数据集与您存储它的方式联系起来。

For a lot of ActiveRecord implementations, the framework does not provide this separation of intent and this can lead to problems. 对于许多ActiveRecord实现,框架不提供意图分离,这可能导致问题。 For example, a BlogPost model can wrap up the basic information of a blog post like 例如,BlogPost模型可以包含博客文章的基本信息

  • title 标题
  • author 作者
  • body 身体
  • date_posted 发布日期

But maybe you also want to have it contain something like: 但也许你也希望它包含类似的东西:

  • number_of_reads number_of_reads
  • number_of_likes number_of_likes

Now you could store all of this data in a single MySQL table to begin with, but as your blog grows and you become super famous, you find out that your statistics data is taking an awful lot of hits and you want to move it off to a separate database server. 现在你可以将所有这些数据存储在一个MySQL表格中,但随着你的博客的发展和你变得超级出名,你会发现你的统计数据遭受了大量的点击,你想把它移到单独的数据库服务器。

How would you go about migrating those fields of the BlogPost objects off to a different data store without changing your application code? 您如何在不更改应用程序代码的情况下将BlogPost对象的这些字段迁移到其他数据存储?

With the DataMapper, you can modify the way the object is saved to the database(s) and the way it is loaded from the database(s). 使用DataMapper,您可以修改对象保存到数据库的方式以及从数据库加载的方式。 This lets you tweak the storage mechanism without having to change the actual collection of information that your application relies on. 这使您可以调整存储机制,而无需更改应用程序所依赖的实际信息集合。

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

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