php - 如何比较 Yii2 ActiveDataProvider 中连接表的值

标签 php yii2

我正在尝试创建一个搜索类来支持 gridview 小部件。问题是我需要比较主表和连接表中的值。
我在搜索类中这样做:

$query = User::find();
$query->joinWith(['rank']);
然后在我的过滤器中,我想要类似的东西:
$query->andFilterWhere(['>=', 'user.rank_points', 'rank.promotion_points']);
但这不起作用,因为第三个参数 rank.promotion_points被转义为字符串,不被视为 mysql 字段。
我曾尝试使用关系输出如下值:
 $query->andFilterWhere(['>=', 'user.rank_points', $this->rank->promotion_points]);
但这给出了一个错误 $this没有 rank属性(property)。
完成此操作的正确方法是什么?
根据要求编辑,这是上述代码生成的原始查询:
SELECT `user`.*
FROM `user`
LEFT JOIN `rank`
ON `user`.`rank_id` = `rank`.`id`
WHERE (`rank_id` NOT IN (1, 2, 3, 4, 5, 6))
AND (`user`.`rank_points` >= 'rank.promotion_points')
但我需要的是这个:
SELECT `user`.*
FROM `user`
LEFT JOIN `rank`
ON `user`.`rank_id` = `rank`.`id`
WHERE (`rank_id` NOT IN (1, 2, 3, 4, 5, 6))
AND (`user`.`rank_points` >= `rank`.`promotion_points`)
整个方法如下所示:
public function search($params)
{
    $query = User::find();
    $query->joinWith(['rank']);

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

    if (!$this->validate()) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    // grid filtering conditions
    $query->andFilterWhere([
        'id' => $this->id,
        'rank_id' => $this->rank_id,
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ]);

    $query->andFilterWhere(['like', 'username', $this->username]);
    $query->andFilterWhere(['not in', 'rank_id', [1, 2, 3, 4, 5, 6]]);
    $query->andFilterWhere(['>=', 'user.rank_points', 'rank.promotion_points']);

    return $dataProvider;
rank表架构:
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | int(11)      | NO   | PRI | NULL    | auto_increment |
| name             | varchar(255) | NO   | UNI | NULL    |                |
| rank_points      | int(11)      | YES  |     | NULL    |                |
| promotion_points | int(11)      | YES  |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+

最佳答案

您可以将此条件作为字符串传递:

$query->andWhere('user.rank_points >= rank.promotion_points');
或使用 Expression :
$query->andWhere(['>=', 'user.rank_points', new \yii\db\Expression('rank.promotion_points')]);

关于php - 如何比较 Yii2 ActiveDataProvider 中连接表的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63662964/

相关文章:

css - Yii2:自定义 LinkPager 以在分页中显示省略号

php - 在一个查询中更新多行性能非常慢

php - 如何将用户绘制的网页中的 SVG 转换为 PNG 或 JPG?

php - 如何解决 PHP 中的 'cannot pass parameter by reference' 错误?

php - 在不知道键的情况下使用单个查询更新 MySQL 中的 100k 行

php exec mysql 空输出

gridview - 在 yii2 中的 gridview pjax 搜索中使用 POST 方法

php - yii2 sqlite 无法解析表名

PHP Yii2密码加密

forms - 我如何获得 form->field() 值?