Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading standard getters

Tags:

yii

I want to make a custom getter that should return two fields of the model instead of one. I have the attribute name like first_name. And the getter I am making is

public function getFirstName(){
        return 1;
}

And then I try to get it called in a CDetailView like this

'client.first_name:raw:Client',

But it returns the standard attribute of the model. How to do it right?

like image 828
Vit Kos Avatar asked Mar 26 '26 02:03

Vit Kos


1 Answers

Yii's order of operations to retrieve an attribute is as follows:

  • AR attribute
  • public variable
  • custom getter

I'm not sure whether AR attributes or public variables are pulled first, but I do know that if either of them exist, your custom getter won't be called.

If you already have a first_name attribute (from AR), then you'll need to use a different name for your getter and use that.

like image 166
acorncom Avatar answered Mar 31 '26 10:03

acorncom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!