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

2025-05-27 0 91

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

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

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

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

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

?

1

2

3

4
public function getStat()

{

return $this->hasOne(UserStat::className(), ['user_id' => 'id']);

}

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
<?= GridView::widget([

'dataProvider' => $dataProvider,

'columns' => [

//其他列

[

'label' => '统计数据',

'value' => function($model){

return isset($model->stat->data) ? $model->stat->data : null;

}

],

//其他列

],

]); ?>

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

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

UserSearch:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52
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就可以这样使用来展示统计数据,就可以排序了

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
<?= GridView::widget([

'dataProvider' => $dataProvider,

'columns' => [

//其他列

[

'label' => '统计数据',

'attribute' => 'data',

'value' => function($model){

return isset($model->stat->data) ? $model->stat->data : null;

}

],

//其他列

],

]); ?>

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

?

1

2

3

4

5

6

7

8

9

10

11
<?php $form = ActiveForm::begin(); ?>

//其他列

<?= $form->field($model, 'data')?>

//其他列

<div class="form-group">

<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>

</div>

<?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

3

4

5

6

7

8

9

10
// 默认是这样的

public static function tableName()

{

return 'user_stat';

}

public static function getDb()

{

return Yii::$app->get('dbStat');

}

?

1

2

3

4

5

6

7

8

9

10
// 只需要在表明前添加数据库名

public static function tableName()

{

return 'memory_stat.user_stat';

}

public static function getDb()

{

return Yii::$app->get('dbStat');

}

?

1

2

3

4

5

6

7

8

9

10

11
// 为了提高代码稳定性,可以这样写

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');

}

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Yii2实现跨mysql数据库关联查询排序功能代码 https://www.kuaiidc.com/73132.html

相关文章

猜你喜欢
发表评论
暂无评论