dongpu42006096 2010-03-31 07:18
浏览 42
已采纳

Zend Dd Rowset获得父行

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条回答 默认 最新

  • douying9296 2010-03-31 08:35
    关注

    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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂