[eluser]mcnux[/eluser]
[quote author="OverZealous.com" date="1248899005"]@mcnux
I'm not at all sure what you are trying to do. I don't recommend manipulating the $fields array, since it is expected to be a direct mapping to the database columns.
If you could provide a more concrete example, I may be able to help you. :-)[/quote]
Sorry, tried to keep it short as I'm good at going into too much detail! Here's an example:
// Vehicle Model
function milesDriven(){
return doLotsOfExpensiveOperationsToGetTheTotal();
}
function timeOnTheRoad(){
return evenMoreExpensive();
}
function averageSpeed(){
return $this->milesDriven() / $this->timeOnTheRoad();
}
As you can see, the expensive calculations/db queries/whatever on milesDriven() and timeOnTheRoad() are executed every time they are called. I would like the expensive operation to occur only once, something like:
Code:
// Vehicle Model
function milesDriven(){
if(!isset($this->miles_driven)){
$this->miles_driven = doLotsOfExpensiveOperationsToGetTheTotal();
}
return $this->miles_driven;
}
function timeOnTheRoad(){
if(!isset($this->time_on_the_road)){
$this->time_on_the_road = evenMoreExpensive();
}
return $this->time_on_the_road;
}
function averageSpeed(){
return $this->milesDriven() / $this->timeOnTheRoad();
}
Obviously I could get this to work quite easily but the problem comes when I move to the next row in the recordset and ask for the same property; I somehow need to reset the cached value every time the row is changed.
Yeah I see now that messing around with Model->fields would have been dangerous! Any ideas?