2012-07-09 60 views
0

我在我的组织模型中有以下功能。Zend框架fetchall函数

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_types模型,所以我可以留下参加组织表organisation_type_id?

回答

1

这是赞成使用数据映射器模式的争论的核心。
由于您在示例中似乎使用了该结构,因此尝试将organisation_types对象传入Organisation模型时会遇到很大的麻烦。但是,您可以在查询中加入organisation_types表,但加入该对象不太合理。

join on the organisation_types table

//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上找到了一些很好的教程,帮助我了解了数据映射器和域模型。

Building A Domain Model, Introduction
Integrating data mappers

而且在线图书Survive The Deepend有数据映射器模式以及如何测试它的一个很好的例子。

好运...