duanjuebin2519 2016-04-15 12:46
浏览 51
已采纳

ZF2 Hydrator和LEFT JOIN

I am hydrating a MySQL result:

public function getMiscdamage($id) 
{
    $sql = new Sql($this->dbAdapter);
    $select = $sql->select('misc_damage');
    $select->where(array('vehicle_id = ?' => $id))->order('date_added DESC');
    $select->join('user','user.user_id = misc_damage.added_user_id', array('added_user_display_name' => 'display_name'), 'left');

    $stmt = $sql->prepareStatementForSqlObject($select);
    $result = $stmt->execute();

    if ($result instanceof ResultInterface && $result->isQueryResult()) {
        $resultSet = new HydratingResultSet(new \Zend\Stdlib\Hydrator\ClassMethods(), new \Application\Model\Miscdamage());

        return $resultSet->initialize($result);
    }

    throw new \InvalidArgumentException("Vehicle with given ID:{$id} not found.");
}

The query has a LEFT join, and the query DOES work. But when I var_dump in a loop through, the added_user_display_name` is null:

/var/www/zf-skeleton/module/Application/view/application/live/details.phtml:337:
object(Application\Model\Miscdamage)[662]
  protected 'id' => string '100' (length=3)
  protected 'vehicle_id' => string '8' (length=1)
  protected 'added_user_id' => string '1' (length=1)
  protected 'added_user_display_name' => null
  protected 'description' => string 'dfgdggfdd' (length=9)
  protected 'date_added' => string '2016-04-15 12:26:40' (length=19)
  protected 'date_repaired' => null
  protected 'repaired_user_id' => null
  protected 'status' => string '0' (length=1)

How do I go about using a left join and hydrator? Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dongtangdao7232 2016-04-16 13:33
    关注

    Your sql result is a response from a functionnal need of data. Therefore, you should use a custom hydrator built specifically for this functionnal need. This hydrator is often an aggregator hydrator which uses multiple hydrator to build a custom model. You should check it out

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据