Skip to content

INSERT with multiple associative arrays is order-dependent and not key-dependent #238

@czycha

Description

@czycha

Version: 2.4.6

Bug Description

When inserting multiple associative arrays, the order in which the keys are defined in the first array is what is used for mapping the rest of the values. It should be based on key instead. I ended up with malformed data because values were ending up in columns they didn't belong in.

Steps To Reproduce

Very trivial example below, but assume that you have this data being constructed based on several conditions wherein the key order is different in the separate branches.

Assuming you have a table named people that has first and last as varchar fields.

<?php
  $people = [
    [
      'first' => 'John',
      'last' => 'Doe'
    ],
    [
      'last' => 'Example',
      'first' => 'Susan',
    ]
  ];

  $db->query('INSERT INTO people', $people);

Check the table and Susan's first and last names are swapped.

Expected Behavior

Should evaluate an insert statement like the following:

INSERT INTO people (first, last) VALUES
("John", "Doe")
("Susan", "Example")

Instead it evaluates like this:

INSERT INTO people (first, last) VALUES
("John", "Doe")
("Example", "Susan")

Possible Solution

Use keys for associative array case as opposed to the order within the array.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions