Yii:添加自定义字段

发布于 2025-01-02 11:08:43 字数 1073 浏览 0 评论 0原文

有没有一种简单的方法可以将自定义字段添加到模型中?假设我有一个表“user”,其中包含 3 个字段:id、name 和 surname。我想要这个:

$user = User::model()->findByPk(1);
$echo $user->fullName; // echoes name and surname

请注意:我希望通过 sql 添加此自定义字段,就像

$c = new CDbCriteria();
$c->select = 'CONCAT("user".name, "user".surname) as fullName'; 
$user = User::model()->find($c);

问题是未设置 fullName 属性。

UPD

这里是一个有点棘手的问题的代码——来自另一个表的自定义字段。它是这样完成的:

    $model = Application::model();
    $model->getMetaData()->columns = array_merge($model->getMetaData()->columns, array('fullName' => 'CONCAT("u".name, "u".surname)'));

    $c = new CDbCriteria();
    $c->select = 'CONCAT("u".name, "u".surname) as fullName';
    $c->join = ' left join "user" "u" on "t".responsible_manager_id = "u".id';

    $model->getDbCriteria()->mergeWith($c);

    foreach ($model->findAll() as $o) {
        echo '<pre>';
        print_r($o->fullName);
        echo '</pre>';
    }

Is there a simple way of adding custom fields to a model? Say I have a table "user" with 3 fields: id, name and surname. I want this:

$user = User::model()->findByPk(1);
$echo $user->fullName; // echoes name and surname

Please note: I want this custom field to be added via sql, smth like

$c = new CDbCriteria();
$c->select = 'CONCAT("user".name, "user".surname) as fullName'; 
$user = User::model()->find($c);

Problem is that fullName property is not set.

UPD:

here is the code for a little bit trickier problem -- custom field from another table. This is how it's done:

    $model = Application::model();
    $model->getMetaData()->columns = array_merge($model->getMetaData()->columns, array('fullName' => 'CONCAT("u".name, "u".surname)'));

    $c = new CDbCriteria();
    $c->select = 'CONCAT("u".name, "u".surname) as fullName';
    $c->join = ' left join "user" "u" on "t".responsible_manager_id = "u".id';

    $model->getDbCriteria()->mergeWith($c);

    foreach ($model->findAll() as $o) {
        echo '<pre>';
        print_r($o->fullName);
        echo '</pre>';
    }

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

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

发布评论

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

评论(2

謌踐踏愛綪 2025-01-09 11:08:43

您可以向 User 类添加一个函数:

public function getFullName() { return $this->name.' '.$this->surname; }

这将返回全名,就好像它是数据库中的属性一样。这比向 SQL 添加计算列要容易得多。

You can add a function to the User class:

public function getFullName() { return $this->name.' '.$this->surname; }

This will return the full name as if it were an attribute from the database. This is much easier than adding a calculated column to the SQL.

掐死时间 2025-01-09 11:08:43

在模型中

public function getMetaData(){
        $data = parent::getMetaData();
        $data->columns['fullName'] = array('name' => 'fullName');
        return $data;
    }

因此不推荐

In model

public function getMetaData(){
        $data = parent::getMetaData();
        $data->columns['fullName'] = array('name' => 'fullName');
        return $data;
    }

Thus not recommended

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