views:

178

answers:

1

I am wondering about using the abstraction in Zend Db RowSet instead of joins, is it possible for instance I am able to get some info from parent table as in here

/**
 * Get default photo info (path , description)
 */
public function getDefaultPhotoInfo($userdId){

    $select = $this->select($this)
            ->where('id=?', $userdId);
    $rowset = $this->fetchAll($select);
    $current = $rowset->current();

    $res = $current->findParentRow('UserPhotos', 'Avatar');
    if(isset($res)){
        return $res->toArray();
    }
}

How can I use Rowset abstraction to get this logic working

table( id, pic_path,) table_translation(id, table_id, lang_id, pic_title);

the above is representation of two tables , the idea is to get the info from both table specifying the lang_id , it is easy with joins but can I do it with the abstraction of Db Rowset ?

+1  A: 

Just for clarification: when doing fetchAll on a Zend_Db_Table instance, you get a Zend_Db_Table_Rowset, which implements the Iterator interface. Thus, calling current() on the Rowset instance, will return a Zend_Db_Table_Row instance.

As of ZF1.10, you define relationships between tables in a Zend_Db_Table_Defintion instance or on a concrete table instance like described in the reference guide for Zend_Db_Table Relationships. Since the guide is rather detailed, I won't reproduce this here.

Once you defined relationships, you can fetch them from a row with (example 1 from guide)

  $accountsTable = new Accounts();
  $accountsRowset = $accountsTable->find(1234);
  $user1234 = $accountsRowset->current();       
  $bugsReportedByUser = $user1234->findDependentRowset('Bugs');

or by the magic finder methods.

The findParentRow() method is somewhat different to that, as it return the full row of a dependent rowset from it's parent row.

Ex5: This example shows getting a Row object from the table Bugs (for example one of those bugs with status 'NEW'), and finding the row in the Accounts table for the user who reported the bug.

  $bugsTable = new Bugs();
  $bugsRowset = $bugsTable->fetchAll(array('bug_status = ?' => 'NEW'));
  $bug1 = $bugsRowset->current();
  $reporter = $bug1->findParentRow('Accounts');

When using table relations, keep in mind that these will result in one additional query per fetched dependent table, whereas a Join does it all in one.

Also see this related questions:

Gordon