On Yii 2.0, use updateAll() to update the implementation of the table column value to another column value of the same table

使用 updateAll() 更新 prev_status 的值为 status 的值,更新 status 的值为 4,表结构
1. Use updateAll() to update the value of prev_status to the value of status, and update the value of status to 4, the table structure is shown in Figure 1
使用 updateAll() 更新 prev_status 的值为 status 的值,更新 status 的值为 4,表结构
Figure 1
2. Based on the query builder, the code is as follows


self::updateAll(['prev_status' => 'status', 'status' => self::PLAN_TASK_STATUS_CLOSE], ['in', 'plan_id', $planId]);


3. However, it does not meet the expectations. The values of prev_status are updated with 0, and the final SQL statement is as follows


UPDATE `pa_plan_task` SET `prev_status`=0, `status`=4 WHERE `plan_id` IN (1, 2)


4. Based on yii\db\expression, it means a db expression that does not need to be escaped or referenced, the code is as follows


use yii\db\Expression;

self::updateAll(['prev_status' => new Expression('status'), 'status' => self::PLAN_TASK_STATUS_CLOSE], ['in', 'plan_id', $planId]);


5. The result is as expected, and the final generated SQL statement is as follows


UPDATE `pa_plan_task` SET `prev_status`=status, `status`=4 WHERE `plan_id` IN (1, 2)


6. The operation result is as expected, as shown in Figure 2
操作结果符合预期
Figure 2

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.