gpt4 book ai didi

php - 在 CGridView 中显示连接表中的列

转载 作者:行者123 更新时间:2023-12-02 07:03:07 24 4
gpt4 key购买 nike

我通过 CDbCriteria 在模型中加入了一些表我的代码是这样的:

$crt = new CDbCriteria();
$crt->alias = 'so';
$crt->select = 'u.id, u.first_name, u.last_name';
$crt->join = " inner join " . Flow::model()->tableName() . " as fl on fl.id = so.flow_id";
$crt->join .= " inner join " . RoleUser::model()->tableName() . " as ru on ru.id = fl.receiver_role_user_id";
$crt->join .= " inner join " . User::model()->tableName() . " as u on ru.user_id= u.id";
$crt->compare('sms_outbox_group_id', $smsOutboxGroupId);
$crt->compare('fl.kind', Flow::KIND_SMS);
$crt->group = 'u.id';

$smsOutBox = new SmsOutbox();
return new CActiveDataProvider($smsOutBox, array(
'criteria' => $crt,
'sort' => array(
'defaultOrder' => 'so.id DESC',
)
));

如何在 CGridView 中显示我选择的列?是否有任何可能的方法来显示 first_namelast_name 而无需在模型中定义关系?

最佳答案

我使用 Sudhanshu Saxena 答案找到了解决方案。除了为 first_namelast_name 使用别名外,我还为模型添加了两个与别名名称相同的属性:receiverFirstNamereceiverLastName。这样我的问题就解决了,此外它还提供了这两个属性的搜索功能。我的最终代码是这样的:

Model :
public $receiverFirstName;
public $receiverLastName;

创建标准:

$crt = new CDbCriteria();
$crt->alias = 'so';
$crt->select = 'so.id,u.id as userId, u.first_name as receiverFirstName, u.last_name as receiverLastName, so.status';
$crt->join = " inner join " . Flow::model()->tableName() . " as fl on fl.id = so.flow_id";
$crt->join .= " inner join " . RoleUser::model()->tableName() . " as ru on ru.id = fl.receiver_role_user_id";
$crt->join .= " inner join " . User::model()->tableName() . " as u on ru.user_id= u.id";
$crt->compare('sms_outbox_group_id', $smsOutboxGroupId);
$crt->compare('u.first_name', $this->receiverFirstName, true);
$crt->compare('u.last_name', $this->receiverLastName, true);
$crt->compare('so.status', $this->status);
$crt->compare('fl.kind', Flow::KIND_SMS);
$crt->group = 'userId';
$crt->order = 'so.id';


return new CActiveDataProvider($this, array(
'criteria' => $crt,
));

最后在 CgridView 中我做了这个:

'columns' => array(
array(
'header' => Yii::t('app', 'Row'),
'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
array(
'name' => 'receiverFirstName',
'value' => '$data->receiverFirstName',
),
array(
'name' => 'receiverLastName',
'value' => '$data->receiverLastName',
),
array(
'name' => 'status',
'value' => 'SmsOutbox::getStatusTitle($data->status)',
'filter' => CHtml::listData(SmsOutbox::getStatusList(), 'id', 'title')
),

),

关于php - 在 CGridView 中显示连接表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17022501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com