public function getStat()
{
return $this->hasOne(UserStat::className(), ['user_id' => 'id']);
}
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
//其他列
[
'label' => '统计数据',
'value' => function($model){
return isset($model->stat->data) ? $model->stat->data : null;
}
],
//其他列
],
]); ?>
public $data;
public function rules()
{/*{{{*/
return [
['data'], 'integer'],
//其他列
];
}/*}}}*/
public function search($params, $onlyActiveUsers = false)
{
$query = User::find();
$query->joinWith(['stat']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'attributes' => [
//其他列
'data' => [
'asc' => [UserStat::tableName() . '.data' => SORT_ASC],
'desc' => [UserStat::tableName() . '.data' => SORT_DESC],
],
//其他列
],
'defaultOrder' => [
'id' => SORT_DESC,
],
],
'pagination' => [
'pageSize' => 50,
],
]);
$this->load($params);
if (!$this->validate()) {
$query->where('0=1');
return $dataProvider;
}
$query->filterWhere([
//其他列
UserStat::tableName() . '.data' => $this->data
]);
return $dataProvider;
}
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
//其他列
[
'label' => '统计数据',
'attribute' => 'data',
'value' => function($model){
return isset($model->stat->data) ? $model->stat->data : null;
}
],
//其他列
],
]); ?>
<?php $form = ActiveForm::begin(); ?>
//其他列
<?= $form->field($model, 'data')?>
//其他列
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
// 默认是这样的
public static function tableName()
{
return 'user_stat';
}
public static function getDb()
{
return Yii::$app->get('dbStat');
}
// 只需要在表明前添加数据库名
public static function tableName()
{
return 'memory_stat.user_stat';
}
public static function getDb()
{
return Yii::$app->get('dbStat');
}
// 为了提高代码稳定性,可以这样写
public static function tableName()
{
preg_match("/dbname=([^;]+)/i", static::getDb()->dsn, $matches);
return $matches[1].'.user_stat';
}
public static function getDb()
{
return Yii::$app->get('dbStat');
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有