PHP Kohana3. Can I rename ORM object properties?
-
08-03-2021 - |
Question
I'm using Kohana3.
For example I create instance:
$user = ORM::factory('orm_user', $user_id)
After this I can get value of each column using this syntax: $user->user_login
or $user->user_password
.
Can I rename this properties to get them like: $user->login
or $user->pass
?
Solution
Yes, you can overload the magic __set and __get methods in your model:
public function __get($field)
{
switch ($field) {
case 'login':
return $this->user_login;
case 'pass':
return $this->user_password;
}
return parent::__get($field);
}
public function __set($field, $value)
{
switch ($field) {
case 'login':
$this->user_login = $value;
return;
case 'pass':
$this->user_password = $value;
return;
}
parent::__set($field, $value);
}
Any rules or filters you write will still need to use the original column names.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow