Take the example of a model with a firstName and lastName.

I know I can add a getFullName method to the model, and $model->fullName will run through the getter.

However, I'd like the attribute to also remain part of the model (accessible through $model->attributes), which if I use the getter, it isn't.

If I use the getter AND include the same name in defineAttributes, the getter is ignored.

Is there a way to achieve this?

1 Answer 1


I think you're out of luck. You can't go through the magic __get() logic and have a concrete get method and use both. How will Yii/PHP know which one you're referring to when you do $model->fullName?

  • Ok, I thought that might be the case.
    – Tim Kelty
    Nov 14, 2014 at 3:16
  • I don't necessarily need it in both places, I just need a way to get the populated model, including fullName, without having to manually add it every time.
    – Tim Kelty
    Nov 14, 2014 at 3:24
  • Not sure I'm following that 100%. Maybe a case for a behavior ? (yiiframework.com/wiki/44/behaviors-events)
    – Brad Bell
    Nov 14, 2014 at 3:28
  • A behavior in Yii is similar to a Ruby mixin or PHP trait if you're familiar with those.
    – Brad Bell
    Nov 14, 2014 at 3:30
  • I'll look at behaviors, thanks. Here's the real example of what I'm trying to do: gist.github.com/timkelty/09b42e50105b6c420c5b
    – Tim Kelty
    Nov 14, 2014 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.