Yii2實(shí)現(xiàn)跨mysql數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢排序功能代碼
背景:在一個(gè)mysql服務(wù)器上(注意:兩個(gè)數(shù)據(jù)庫(kù)必須在同一個(gè)mysql服務(wù)器上)有兩個(gè)數(shù)據(jù)庫(kù):
memory (存儲(chǔ)常規(guī)數(shù)據(jù)表) 中有一個(gè) user 表(記錄用戶信息)
memory_stat (存儲(chǔ)統(tǒng)計(jì)數(shù)據(jù)表) 中有一個(gè) user_stat (記錄用戶統(tǒng)計(jì)數(shù)據(jù))
現(xiàn)在在 user 表生成的 GridView 列表中展示 user_stat 中的統(tǒng)計(jì)數(shù)據(jù)
只需要在User的model類中添加關(guān)聯(lián)
public function getStat() { return $this->hasOne(UserStat::className(), ['user_id' => 'id']); }
在GridView就可以這樣使用來(lái)展示統(tǒng)計(jì)數(shù)據(jù)
<?= GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ //其他列 [ 'label' => '統(tǒng)計(jì)數(shù)據(jù)', 'value' => function($model){ return isset($model->stat->data) ? $model->stat->data : null; } ], //其他列 ], ]); ?>
現(xiàn)在增加了一個(gè)需求,需要在user GridView 列表中對(duì)統(tǒng)計(jì)數(shù)據(jù)進(jìn)行排序和篩選
若 user 和 user_stat 表在同一個(gè)數(shù)據(jù)庫(kù)下我們可以這樣做:
UserSearch:
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就可以這樣使用來(lái)展示統(tǒng)計(jì)數(shù)據(jù),就可以排序了
<?= GridView::widget([ 'dataProvider' => $dataProvider, 'columns' => [ //其他列 [ 'label' => '統(tǒng)計(jì)數(shù)據(jù)', 'attribute' => 'data', 'value' => function($model){ return isset($model->stat->data) ? $model->stat->data : null; } ], //其他列 ], ]); ?>
search 表單中添加以下列就可以篩選了
<?php $form = ActiveForm::begin(); ?> //其他列 <?= $form->field($model, 'data')?> //其他列 <div class="form-group"> <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?>
然而現(xiàn)實(shí)是殘酷的, user 和 user_stat 表并在同一個(gè)數(shù)據(jù)庫(kù)下。
于是就會(huì)報(bào)出這樣一個(gè)錯(cuò)誤:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'memory.user_stat' doesn't exist
The SQL being executed was: ...
要在兩個(gè)數(shù)據(jù)庫(kù)(同一臺(tái)服務(wù)器)上進(jìn)行關(guān)聯(lián)數(shù)據(jù)查詢,純SQL語(yǔ)句如下:
select a.*,b.* from memory.user as a,memory_stat.user_stat as b where a.id=b.user_id;
Yii2轉(zhuǎn)化成 SQL 語(yǔ)句時(shí)默認(rèn)不會(huì)在表明前添加數(shù)據(jù)庫(kù)名,于是mysql在執(zhí)行sql語(yǔ)句時(shí)就會(huì)默認(rèn)此表在memory數(shù)據(jù)庫(kù)下。
select a.*,b.* from memory.user as a,memory.user_stat as b where a.id=b.user_id;
于是就出現(xiàn)了以上報(bào)錯(cuò)信息。
那么,如何來(lái)解決這個(gè)問(wèn)題呢?
其實(shí)很簡(jiǎn)單,只需要重寫(xiě) user_stat 的 model 類下的 tableName() 方法就可以了。
// 默認(rèn)是這樣的 public static function tableName() { return 'user_stat'; } public static function getDb() { return Yii::$app->get('dbStat'); }
// 只需要在表明前添加數(shù)據(jù)庫(kù)名 public static function tableName() { return 'memory_stat.user_stat'; } public static function getDb() { return Yii::$app->get('dbStat'); }
// 為了提高代碼穩(wěn)定性,可以這樣寫(xiě) 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'); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)(php)
直接給出源代碼,可自行分析,寫(xiě)的不好請(qǐng)留言指正,謝謝!2010-07-07解決laravel資源加載路徑設(shè)置的問(wèn)題
今天小編就為大家分享一篇解決laravel資源加載路徑設(shè)置的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10php post json參數(shù)的傳遞和接收處理方法
今天小編就為大家分享一篇php post json參數(shù)的傳遞和接收處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05ThinkPHP框架實(shí)現(xiàn)導(dǎo)出excel數(shù)據(jù)的方法示例【基于PHPExcel】
這篇文章主要介紹了ThinkPHP框架實(shí)現(xiàn)導(dǎo)出excel數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了thinkPHP添加org擴(kuò)展基于PHPExcel進(jìn)行Excel數(shù)據(jù)的導(dǎo)出操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-05-05eaglephp使用微信api接口開(kāi)發(fā)微信框架
EaglePHP框架開(kāi)發(fā)微信5.0的API接口,包含微信5.0 API基礎(chǔ)接口、自定義菜單、高級(jí)接口,包括如下接收用戶消息、向用戶回復(fù)消息、會(huì)話界面自定義菜單、語(yǔ)音識(shí)別、客服接口等功能2014-01-01