PHP Kohana3。我可以重命名 ORM 对象属性吗?

发布于 2024-12-17 16:59:31 字数 269 浏览 4 评论 0原文

我正在使用 Kohana3。 例如我创建实例:
$user = ORM::factory('orm_user', $user_id)
之后,我可以使用以下语法获取每列的值:$user->user_login$user->user_password
我可以重命名此属性以使其类似于:$user->login$user->pass 吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉墨 2024-12-24 16:59:31

是的,您可以在模型中重载神奇的 __set 和 __get 方法:

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);
}

您编写的任何规则或过滤器仍然需要使用原始列名称。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文