Getter-method for attribute (DB column) does not work

Hi all.

I have a table USER with column USERNAME. I created model User and if I want to access a particular username I do it like this: $user->username. It’s clear. For example value “admin” is returned.

But now I want to change the way usernames are shown. For example I want to use UPPERCASE. I think that this should be easily done by a getter-method, but it doesn’t work:




public function getUsername()

{

  return strtoupper($this->username);

}



Now I expect that $user->username will not return the real value from DB but will run the getter-method.

What am I doing wrong? I am using Yii 1.1.16

U will get Your method result only if u call $user->getUserName(), otherwise U will get DB value. Issue here is that attribute name and your method name (without ‘get’) so U will allways fall into DB value. U have to either use getUsername() or think of another method name, for example getUser(), than U can use $user->user for getting method result

1 Like

Thanks, this is what I was afraid of :slight_smile:

1 Like