最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Yii2實(shí)現(xiàn)跨mysql數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢排序功能代碼

 更新時(shí)間:2017年02月10日 16:20:47   作者:白菜1031  
本篇文章主要介紹了Yii2實(shí)現(xiàn)跨mysql數(shù)據(jù)庫(kù)關(guān)聯(lián)查詢排序功能示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

背景:在一個(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類(lèi)中添加關(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ǔ)句如下:

復(fù)制代碼 代碼如下:

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ù)下。

復(fù)制代碼 代碼如下:

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 類(lèi)下的 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)文章

最新評(píng)論

楚雄市| 石景山区| 泗水县| 安阳市| 沁源县| 措勤县| 平阳县| 安顺市| 苗栗市| 安仁县| 治多县| 莲花县| 都匀市| 海兴县| 穆棱市| 慈利县| 县级市| 新平| 夏津县| 留坝县| 正镶白旗| 宁津县| 金昌市| 乐山市| 买车| 柳林县| 宁化县| 光山县| 进贤县| 铜鼓县| 建德市| 东乌珠穆沁旗| 平度市| 凉山| 家居| 西峡县| 罗定市| 临清市| 宁陕县| 双辽市| 楚雄市|