Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
czycha opened this issue Nov 8, 2019 · 0 comments
Closed

Comments

@czycha
Copy link

@czycha czycha commented Nov 8, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.