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

该查询具有LEFT联接,并且该查询确实有效。但当我在循环中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结果是函数对数据需求的响应。因此,您应该使用专门为这一功能需求而定制的水合器。这种水合器通常是一种聚合器水合器,它使用多个水合器来构建自定义模型。你应该看看