0

組織モデルに以下の機能があります。

public function getOrganisations($where=null,$order='name ASC',$offset=null,$limit=null){
            $Result = $this->fetchAll($where,$order,$limit,$offset);
            if (!$Result){
                return array();
            }

            return $Result->toArray();
        }

しかし、組織テーブルのorganisation_type_idに結合したままにできるように、organisation_typesモデルを含めるにはどうすればよいですか?

4

2 に答える 2

1

これが、データ マッパー パターンの使用を支持する議論の核心です。例で使用しているように見える構造では、オブジェクトをモデルに
渡そうとすると大きな問題が発生します。ただし、クエリで結合を実行してテーブルに結合することはできますが、オブジェクトでの結合は合理的ではありません。organisation_typesOrganisationorganisation_types

organization_types テーブルに参加するには:

//assuming this is a DbTable model that extends Zend_Db_Table_Abstract
 function getOrganisations($where=null,$order='name ASC',$offset=null,$limit=null){
     $select = $this->select()->setIntegrityCheck(FALSE);//This locks the table to allow joins
     $select->joinLeft('organisation_types', 'organisation_types.id = organisation.organisation_type_id');//This will join the tables with all feilds, use the join type you like.
     if (!is_null($where) {
         $select->where($where);
     }
     if (!is_null($order) {
         $select->order($order);
     }
     if (!is_null($offset) {
         $select->limit(null,$offset);//offset is second arg in limit() in select()
     }
     if (!is_null($limit) {
         $select->limit($limit);
     }
       $Result = $this->fetchAll($select);
       if (!$Result){
            return array();
       }

       return $Result->toArray();
 }

これにより、テーブル結合がどのように機能するかがわかります。オブジェクトを使用したい場合は、別の構造でやり直す必要があります。
PHPMaster で、データ マッパーとドメイン モデルを理解するのに役立ついくつかの優れたチュートリアルを見つけました。

ドメイン モデルの構築、はじめに
データ マッパーの統合

また、オンライン ブックのSurvive The Deependには、データ マッパー パターンとそのテスト方法の良い例があります。

幸運を...

于 2012-07-09T05:50:26.437 に答える
0

おそらくZend_Db_Selectwithを使用するjoinLeft()方が適切でしょう:

http://framework.zend.com/manual/en/zend.db.select.html#zend.db.select.building.join

于 2012-07-09T05:09:12.320 に答える