Reputation: 2476
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.
Upvotes: 0
Views: 139
Reputation: 1757
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
Upvotes: 0