I have a Behavior attached to a Model that should behave differently depending on some property of the model. Example:
class Airplane extends AppModel {
var $actsAs = array('Flying');
}
class FlyingBehavior extends ModelBehavior {
function flightTime(&$Model, $distance) {
return $distance / $this->speed;
}
}
Initially I thought I setting it like
class Airplane extends AppModel {
var $actsAs = array('Flying' =>
array('speed' => SOMENUM)
);
}
class FlyingBehavior extends ModelBehavior {
function setup(&$Model, $settings) {
$this->speed = $settings['speed'];
}
function flightTime(&$Model, $distance) {
return $distance / $this->speed;
}
}
But I don’t know how to make this work, because I’d need to fetch the speed column from each Airplane record. How should I do this?
I solved the problem by adding code to the
afterFind()callback; setting a property in the Model.I then just need to access it via
$Model->propertyfrom the Behavior. This should be possible without having to meddle withafterFind()but unfortunately I didn’t find the way to do it.