簡體   English   中英

數據映射器和zend框架

[英]Data mapper and zend framework

我正在我的Zend Framework Web應用程序中實現Data Mapper設計模式,一切順利,我真的很高興使用Zend中的數據映射器而不僅僅應用Row Gateway模式,但我的架構有問題。 我不確定如何使用我的數據映射器以OOP方式引用和處理外鍵約束。 所以我有我的Model類,我的DbTable類和我的Mapper類。 我應該將所有外鍵關系放在我的DbTable類中,這樣我可以使用findDependentRowset()函數在我的映射器中檢索,或者更好的方法是在我的映射器中實例化依賴類。 使用Data Mapper模式映射外鍵的最佳OOP實踐是什么?

我會選擇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.

這有任何意義嗎?

findDependentRowset在我的系統上有findDependentRowset 但不是了! 在大多數情況下,這是浪費資源。 表連接應該在您的SQL語句上。

請參閱我的回答: 手工查詢vs findDependentRowset

我還遠離使用Doctrine或Propel(我從來不需要它)。 也許有一天。 (現在使用Doctrine 2.所以......我建議你現在一樣)


老答復

在使用Zend Framework幾年后,我遇到了以下架構:

1)我有一個抽象的映射器,我的所有映射器都擴展了: Zf_Model_DbTable_Mapper

2)我也有一個抽象模型(域對象),類似於行數據網關,但它不是網關。 它是一個通用的域對象: Zf_Model

3)當填充對象圖(SQL連接)時,一個映射器委托給負責給定對象的其他映射器。

此方法來自抽象映射器:

    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