Skip to content

Add method batchUpdate in \yii\db\QueryBuilder #92

Description

@dimmitri

Very often in practice I have to do a bulk update of rows.

IN PostgreSQL:

UPDATE table AS t
SET
    col1 = c.col1,
    col2 = c.col2
FROM (VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4) ) AS c (id, col1, col2)
WHERE c.id = t.id;

or

INSERT INTO table (id, col1, col2) 
      VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4)
ON CONFLICT (id) DO UPDATE SET 
  col1 = EXCLUDED.col1,
  col2 = EXCLUDED.col2

IN MySQL:

INSERT INTO table (id, col1, col2)
     VALUES (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4)
ON DUPLICATE KEY UPDATE
    col1 = VALUES(col1),
    col2 = VALUES(col2);

I'm sure other databases have similar capabilities.
For each DBMS will have its own specific implementation of the method batchUpdate in descendant classes.

Sorry for my bad english.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions