From c70f274ca51a34d2664c01d66538b477a165d806 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Thu, 24 Aug 2017 10:40:45 +0200 Subject: [PATCH] added example to db migration query builder usage --- docs/guide/db-migrations.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/guide/db-migrations.md b/docs/guide/db-migrations.md index 09738a3954..5cbf6a35ec 100644 --- a/docs/guide/db-migrations.md +++ b/docs/guide/db-migrations.md @@ -704,8 +704,17 @@ Below is the list of all these database accessing methods: * [[yii\db\Migration::dropCommentFromTable()|dropCommentFromTable()]]: dropping comment from table > Info: [[yii\db\Migration]] does not provide a database query method. This is because you normally do not need - to display extra message about retrieving data from a database. It is also because you can use the powerful - [Query Builder](db-query-builder.md) to build and run complex queries. +> to display extra message about retrieving data from a database. It is also because you can use the powerful +> [Query Builder](db-query-builder.md) to build and run complex queries. +> Using Query Builder in a migration may look like this: +> +> ```php +> // update status field for all users +> foreach((new Query)->from('users')->each() as $user) { +> $this->update('users', ['status' => 1], ['id' => $user['id']); +> } +> ``` + > Note: When manipulating data using a migration you may find that using your [Active Record](db-active-record.md) classes > for this might be useful because some of the logic is already implemented there. Keep in mind however, that in contrast