How can I update field with query builder in Yii2? I can't find this in documentation.
Thanks!
UPD
This is the solution:
// UPDATE $connection = Yii::$app->db; $connection->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();
Like other answers have pointed out, you can call update() on the builder object. Yii::$app->db->createCommand()->update('tableName', ['field' => 'value'], condition)->execute() . The 2nd parameter is a key-value pair of the fields you want to set and the values you want to set them to.
Using Query Builder, you can search and filter database objects, select objects and columns, create relationships between objects, view formatted query results, and save queries with little or no SQL knowledge.
Create command can be used directly as follows :
\Yii::$app->db->createCommand("UPDATE table SET column1=:column1, column2=:column2 WHERE id=:id") ->bindValue(':id', your_id) ->bindValue(':column1', :column1_value) ->bindValue(':column2', :column2_value) ->execute();
Query builder is for select queries only (sum, max, count too). You should use other methods - AR or raw queries (https://github.com/yiisoft/yii2/blob/master/docs/guide/db-dao.md#basic-sql-queries)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With