首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Zend框架fetchall函数

Zend框架fetchall函数
EN

Stack Overflow用户
提问于 2012-07-09 13:00:57
回答 2查看 5.5K关注 0票数 0

我在我的组织模型中有下面的函数。

代码语言:javascript
复制
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模型,以便我可以在组织表中离开join到organisation_type_id?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-09 13:50:26

这是支持使用数据映射器模式的争论的核心。

使用您在示例中似乎正在使用的结构,您将很难尝试将organisation_types对象传递到Organisation模型中。但是,您可以在查询中执行连接以连接到organisation_types表,但是在对象上连接可能并不合理。

转到join on the organisation_types table

代码语言:javascript
复制
//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有一个很好的数据映射器模式示例以及如何对其进行测试。

祝你好运。

票数 1
EN

Stack Overflow用户

发布于 2012-07-09 13:09:12

也许在joinLeft()中使用Zend_Db_Select会更合适:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11389306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档