我在我的组织模型中有下面的函数。
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?
发布于 2012-07-09 13:50:26
这是支持使用数据映射器模式的争论的核心。
使用您在示例中似乎正在使用的结构,您将很难尝试将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有一个很好的数据映射器模式示例以及如何对其进行测试。
祝你好运。
发布于 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
https://stackoverflow.com/questions/11389306
复制相似问题