-
-
Notifications
You must be signed in to change notification settings - Fork 41
Description
Clarification
There is more at play here than the "order of elements" argument:
- Associative arrays are conceptually like dictionaries. While they have an order in PHP, it is not intuitive to use to depend on it.
- In this bug specifically, if you use associative data rows then type casting will not working unless your columns array also uses the same indexes; this is even less intuitive.
The columns parameter is the ordered list of columns, their indexes shouldn't matter.
Regardless of the argument that you can construct all kinds of arrays in PHP I propose documenting a preferred structure and actually checking if the code conforms...
What steps will reproduce the problem?
Create code like this:
$x->batchInsert('table1', ['a', 'b'], ['b' => 123, 'a' => 345]);What is the expected result?
I expect it to work properly, ie create a query like this:
insert into `table1` (`a`, `b`) values (345, 123);What do you get instead?
insert into `table1` (`a`, `b`) values (123, 345);Solution
The thing that is wrong here is not so much the code as my assumption: batch insert does not support associative arrays; this means things like column order and also column type casting will go wrong when passing in associative arrays.
My proposal is to actively check each key in the row data and throw a hard exception if it's not numerical. Since we're already iterating we can do this without a significant performance cost; the only lines we'd add are something like this:
if (!is_int($i)) {
throw new SomeKindOfException('Data rows must be numerically indexed');
}Additional info
| Q | A |
|---|---|
| Yii version | latest |
| PHP version | N/A |
| Operating system | N/A |
| Related issues | #14608 |