ZF2 Hydrator 和 LEFT JOIN

ZF2 Hydrator and LEFT JOIN

我正在补水 MySQL 结果:

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.");
}

查询有左连接,并且查询有效。但是当我 var_dump 循环时, added_user_display_name` 为空:

/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)

如何使用左连接和水合器?感谢任何帮助。

您的 sql 结果是对数据功能需求的响应。因此,您应该使用专为满足此功能需求而打造的定制保湿器。该水合器通常是聚合器水合器,它使用多个水合器来构建自定义模型。你应该看看