jpablo78
(Jpablo)
1
Hi! How can I use a calculated attribute in AR, the sum of two database fields, for instance?
I tried to add a method in the model class like this:
abstract class BaseClase extends ActiveRecord {
public static function getTotal() {
return $this->minor + $this->major;
}
My test code is:
$clase = Clase::model()->findByPk(1);
echo $clase->othervalue; //This attribute reference works fine
echo $clase->total; //This raises an error.
I get this error: Using $this when not in object context
What I’m doing wrong? ‘minor’, ‘major’, and ‘othervalue’ are fields in the database and attributes of the AR.
zilles
(Zilles)
2
Your function should not be “static”. Just remove that word from getTotal() and you’re good.
jpablo78
(Jpablo)
3
Oh, i’m a dork! I copied it from another method and never realizes about the static modificator. Thank you Karl!