setting a Calculated variable in Model
I have a calculated variable defined in Dairies model - public $sexLabel;
Also have functions to retieve sex of dairy which works as below
public static function getSexLabels()
{
return [
self::MALE => 'Bull',
self::FEMALE => 'Heifer',
];
}
public function getSexLabel()
{
$lables = self::getSexLabels();
if (isset($lables[$this->sex])) {
$this->sexLabel = $lables[$this->sex];
//echo 'sexLabel: '.$this->sexLabel."<br>";
return $lables[$this->sex];
} else {
return "invalid sex ({$this->sex})";
}
}
problem is $sexLabel variable is not available in views and so i have to call the function getSexLabel().
Is their a way to set $sexLabel so i can retrieve in view as $model->$sexLabel, Or is their a function that can be called initialise calculated variables
Thanks