- 相關(guān)推薦
Yii2如何實(shí)現(xiàn)跨mysql數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢排序
導(dǎo)語(yǔ):Yii2如何實(shí)現(xiàn)跨mysql數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢排序呢?下面是小編給大家提供的代碼實(shí)現(xiàn)教程,大家可以參考閱讀,更多詳情請(qǐng)關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
背景:在一個(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就可以這樣使用來展示統(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就可以這樣使用來展示統(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')?>
//其他列
<p class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
</p>
<?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ò)信息。
那么,如何來解決這個(gè)問題呢?
其實(shí)很簡(jiǎn)單,只需要重寫 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)定性,可以這樣寫
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');
}
【Yii2如何實(shí)現(xiàn)跨mysql數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢排序】相關(guān)文章:
PHP 中 MySQL 數(shù)據(jù)庫(kù)異步查詢實(shí)現(xiàn)08-22
如何實(shí)現(xiàn)yii2 數(shù)據(jù)庫(kù)讀寫分離配置07-01
如何實(shí)現(xiàn)歸并排序10-04
php基礎(chǔ)之連接mysql數(shù)據(jù)庫(kù)和查詢數(shù)據(jù)07-30
關(guān)于php操作mysql執(zhí)行數(shù)據(jù)庫(kù)查詢08-11
PHP獲取MySQL數(shù)據(jù)庫(kù)里所有表的實(shí)現(xiàn)代碼08-27
PHP與MYSql連接與查詢06-19
php查詢mysql的實(shí)例09-09