简体   繁体   English

数据映射器和zend框架

[英]Data mapper and zend framework

I am implementing the Data Mapper design pattern in my Zend Framework web application, and everything goes well, I am really enjoying working with the data mapper in Zend and not applying only the Row Gateway pattern, but I am having a problem regarding my architecture. 我正在我的Zend Framework Web应用程序中实现Data Mapper设计模式,一切顺利,我真的很高兴使用Zend中的数据映射器而不仅仅应用Row Gateway模式,但我的架构有问题。 I am not sure how to reference and work with foreign key constraints in a OOP fashion with my data mapper. 我不确定如何使用我的数据映射器以OOP方式引用和处理外键约束。 So I am having my Model class, my DbTable class and my Mapper class. 所以我有我的Model类,我的DbTable类和我的Mapper类。 Should I put all the foreign keys relations in my DbTable class and in this way I can retrieve in my mapper using findDependentRowset() function, or a better approach would be to instantiate the dependent class in my mapper. 我应该将所有外键关系放在我的DbTable类中,这样我可以使用findDependentRowset()函数在我的映射器中检索,或者更好的方法是在我的映射器中实例化依赖类。 What is the best OOP practice in mapping foreign keys using the Data Mapper pattern? 使用Data Mapper模式映射外键的最佳OOP实践是什么?

I'd go for the DataMapper as the other two shoudln't be aware of the ID per se althought it's always necessary for the relationship callers. 我会选择DataMapper,因为其他两个人本身并不知道ID,因为关系调用者总是需要它。

Model
- Property accessors and private property data holders
- Functions regarding correct usage of model
- Relatioship callers (calls relationship fetches in the DbTable to get parents or children rows)or returns cached data

DbTable
- Provides all static functions used to query data and instanciate the related Models such as :getById, getByName, getByComplexSearch, etc
- Provides all static relationship loaders such as: getParents, getChildrens and any other version you need

DataMapper
- Does the real meat and provides the getObjects method which accepts either a query part or a full query and loads the data into the ModelObjects and reads it back into a update, insert, delete query when needed
- Datamapper should be in charge of checking for relationship faults when inserting, updating or deleting using transactions if in InnoDB.

Does that make any sense? 这有任何意义吗?

I used to have findDependentRowset on my systems. findDependentRowset在我的系统上有findDependentRowset But not anymore! 但不是了! It's a waste of resources in most cases. 在大多数情况下,这是浪费资源。 Table joins should be on your SQL statement. 表连接应该在您的SQL语句上。

See my answer here: Hand made queries vs findDependentRowset 请参阅我的回答: 手工查询vs findDependentRowset

I'm still far away from use Doctrine or Propel (I've never needed it). 我还远离使用Doctrine或Propel(我从来不需要它)。 Maybe some day. 也许有一天。 (Now using Doctrine 2 . So... I suggest you the same now) (现在使用Doctrine 2.所以......我建议你现在一样)


OLD ANSWER 老答复

After a couple of years working with Zend Framework, I come across with the following architecture: 在使用Zend Framework几年后,我遇到了以下架构:

1) I have an abstract mapper which all my mappers extends: Zf_Model_DbTable_Mapper 1)我有一个抽象的映射器,我的所有映射器都扩展了: Zf_Model_DbTable_Mapper

2) I have an abstract model (domain object) too, similar to the Row Data Gateway, with the exception that it is not a gateway. 2)我也有一个抽象模型(域对象),类似于行数据网关,但它不是网关。 It's a generic domain object: Zf_Model 它是一个通用的域对象: Zf_Model

3) When populating object graphs (SQL joins) one mapper delegates to the other mappers that are responsible for the given object. 3)当填充对象图(SQL连接)时,一个映射器委托给负责给定对象的其他映射器。

Example

This method is from the abstract mapper: 此方法来自抽象映射器:

    public function getById($id) 
    {


        $tableGateway = $this->getTable();

        $row = $tableGateway->fetchRow('id =' . (int) $id);

        if (!$row) 
            retur null;

        $row = $row->toArray();

        $objectName = $this->getDomainObjectName();
        $object = new $objectName($row['id']);
        $object->populate($row);

        return $object;    
    }

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

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