当前位置:Gxlcms > PHP教程 > Yii2实现跨mysql数据库关联查询排序功能代码

Yii2实现跨mysql数据库关联查询排序功能代码

时间:2021-07-01 10:21:17 帮助过:5人阅读

本篇文章主要介绍了Yii2实现跨mysql数据库关联查询排序功能示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

背景:在一个mysql服务器上(注意:两个数据库必须在同一个mysql服务器上)有两个数据库:

memory (存储常规数据表) 中有一个 user 表(记录用户信息)

memory_stat (存储统计数据表) 中有一个 user_stat (记录用户统计数据)

现在在 user 表生成的 GridView 列表中展示 user_stat 中的统计数据

只需要在User的model类中添加关联

  1. public function getStat()
  2. {
  3. return $this->hasOne(UserStat::className(), ['user_id' => 'id']);
  4. }

在GridView就可以这样使用来展示统计数据

  1. <?= GridView::widget([
  2. 'dataProvider' => $dataProvider,
  3. 'columns' => [
  4. //其他列
  5. [
  6. 'label' => '统计数据',
  7. 'value' => function($model){
  8. return isset($model->stat->data) ? $model->stat->data : null;
  9. }
  10. ],
  11. //其他列
  12. ],
  13. ]); ?>

现在增加了一个需求,需要在user GridView 列表中对统计数据进行排序和筛选

若 user 和 user_stat 表在同一个数据库下我们可以这样做:

UserSearch:

  1. public $data;
  2. public function rules()
  3. {/*{{{*/
  4. return [
  5. ['data'], 'integer'],
  6. //其他列
  7. ];
  8. }/*}}}*/
  9. public function search($params, $onlyActiveUsers = false)
  10. {
  11. $query = User::find();
  12. $query->joinWith(['stat']);
  13. $dataProvider = new ActiveDataProvider([
  14. 'query' => $query,
  15. 'sort' => [
  16. 'attributes' => [
  17. //其他列
  18. 'data' => [
  19. 'asc' => [UserStat::tableName() . '.data' => SORT_ASC],
  20. 'desc' => [UserStat::tableName() . '.data' => SORT_DESC],
  21. ],
  22. //其他列
  23. ],
  24. 'defaultOrder' => [
  25. 'id' => SORT_DESC,
  26. ],
  27. ],
  28. 'pagination' => [
  29. 'pageSize' => 50,
  30. ],
  31. ]);
  32. $this->load($params);
  33. if (!$this->validate()) {
  34. $query->where('0=1');
  35. return $dataProvider;
  36. }
  37. $query->filterWhere([
  38. //其他列
  39. UserStat::tableName() . '.data' => $this->data
  40. ]);
  41. return $dataProvider;
  42. }

在GridView就可以这样使用来展示统计数据,就可以排序了

  1. <?= GridView::widget([
  2. 'dataProvider' => $dataProvider,
  3. 'columns' => [
  4. //其他列
  5. [
  6. 'label' => '统计数据',
  7. 'attribute' => 'data',
  8. 'value' => function($model){
  9. return isset($model->stat->data) ? $model->stat->data : null;
  10. }
  11. ],
  12. //其他列
  13. ],
  14. ]); ?>

search 表单中添加以下列就可以筛选了

  1. <?php $form = ActiveForm::begin(); ?>
  2. //其他列
  3. <?= $form->field($model, 'data')?>
  4. //其他列
  5. <p class="form-group">
  6. <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
  7. </p>
  8. <?php ActiveForm::end(); ?>

然而现实是残酷的, user 和 user_stat 表并在同一个数据库下。

于是就会报出这样一个错误:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'memory.user_stat' doesn't exist
The SQL being executed was: ...

要在两个数据库(同一台服务器)上进行关联数据查询,纯SQL语句如下:


代码如下:


select a.*,b.* from memory.user as a,memory_stat.user_stat as b where a.id=b.user_id;


Yii2转化成 SQL 语句时默认不会在表明前添加数据库名,于是mysql在执行sql语句时就会默认此表在memory数据库下。


代码如下:


select a.*,b.* from memory.user as a,memory.user_stat as b where a.id=b.user_id;


于是就出现了以上报错信息。

那么,如何来解决这个问题呢?

其实很简单,只需要重写 user_stat 的 model 类下的 tableName() 方法就可以了。

  1. // 默认是这样的
  2. public static function tableName()
  3. {
  4. return 'user_stat';
  5. }
  6. public static function getDb()
  7. {
  8. return Yii::$app->get('dbStat');
  9. }

  1. // 只需要在表明前添加数据库名
  2. public static function tableName()
  3. {
  4. return 'memory_stat.user_stat';
  5. }
  6. public static function getDb()
  7. {
  8. return Yii::$app->get('dbStat');
  9. }

  1. // 为了提高代码稳定性,可以这样写
  2. public static function tableName()
  3. {
  4. preg_match("/dbname=([^;]+)/i", static::getDb()->dsn, $matches);
  5. return $matches[1].'.user_stat';
  6. }
  7. public static function getDb()
  8. {
  9. return Yii::$app->get('dbStat');
  10. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHP中文网。

更多Yii2实现跨mysql数据库关联查询排序功能代码相关文章请关注PHP中文网!

人气教程排行