John Kim
John Kim

Reputation: 1882

Zend framework fetchall function

I've got below function in my Organisation model.

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();
        }

but How can I include my organisation_types model so I can left join to organisation_type_id in organisation table?

Upvotes: 0

Views: 5535

Answers (2)

RockyFord
RockyFord

Reputation: 8519

This is the core of the argument in favor of using the data mapper pattern.
With the structure you seem to be using in your example you'll have huge trouble trying pass the organisation_types object into your Organisation model. You can however do a join in your query to join to the organisation_types table, but joining on the object is not likely to be reasonable.

to 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();
 }

This should give you an idea of how a table join would work. If you want to use the objects you'll need to begin again with a different structure.
I found a couple of good tutorials on PHPMaster that helped me get my head around the data mappers and domain models.

Building A Domain Model, Introduction
Integrating data mappers

Also The online book Survive The Deepend has a good example of the data mapper pattern and how to test it.

Good Luck...

Upvotes: 1

uloBasEI
uloBasEI

Reputation: 4215

Maybe using Zend_Db_Select with joinLeft() would be more appropriate:

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

Upvotes: 0

Related Questions