[eluser]OverZealous[/eluser]
[quote author="jpi" date="1248903820"]$vehicles->all is an array where each row is a record in your database AND a vehicle object. When a vehicle objet is instantiate the property time_on_the_road and miles_driven should be empty (as they dont refer to a column in your DB). There is no "cached value".[/quote]
Basically, what he said! You can simply set the field on the objects directly.
If you wanted to make this really seamless, try using the magic __get method:
Code:
// in your Vehicle class
// This method is only called if $field does not exist (PHP feature)
function __get($field) {
// Important! Let DMZ handle everything else
return parent::__get($field);
}
Then to use, you just do this:
Code:
echo $car->milesDriven;
The great thing about __get is it is only called if the field is not already defined. So, the field will always only be calculated the first time it is called, and the pre-calculated value is returned every time after that.
To force it to be re-calculated, you just call unset($car->milesDriven);