Suppose I have a query that returns the following data:
RangeId | MinValue | MaxValue | Resolution | UnitId | UnitName
I want to hydrate the object MeasurementRange
with the above data.
class MeasurementRange {
public function getRangeId() {...};
public function setRangeId($id) {...};
public function getRange() {...};
public function setRange(Range $range) {...};
public function getUnit() {...};
public function setUnit(Unit $unit) {...};
}
class Range {
public function getMinValue() {...};
public function setMinValue(float $minVal) {...};
public function getMaxValue() {...};
public function setMaxValue(float $maxVal) {...};
public function getResolution {...};
public function setResolution(float $resolution) {...};
}
class Unit {
public function getUnitId() {...};
public function setUnitId(int $id) {...};
public function getUnitName() {...};
public function setUnitName(string $name) {...};
}
As you can see the MeasurementRange
object has set Range
and Unit
objects.
How can I hydrate MeasurementRange
and the inner Range
and Unit
objects from the above query?
PS: I didn't specify protected properties of the objects. I guess they are self-evident.
You need to create a mapper, that will use your dbAdapter to fetch the data as an array, and then use hydrators to hydrate all the objects, and finally add the
Range
andUnit
toMeasurementRange
. You could alternatively create a custom hydrator (that would be even better, in terms of single responsibility).I haven't got time to clean the example below, but that's what it could look like :)