D
D
ddddd tttt2018-03-19 05:49:57
Yii
ddddd tttt, 2018-03-19 05:49:57

How to map data to fields in bacheInsert?

public static  $columnsInsert =
        [
            'id',
            'customer_id',
            'summary',
            'description',
            'color',
            'favourite',
            'update_at',
            'create_at',
            'personEmail'
        ];


    $sql = $db->queryBuilder->batchInsert(self::$table, self::$columnsInsert, $data);

An array of strings comes to the server, the string contains the same keys as the base fields. But for some reason the data is not compared, do you really have to compare the fields with the orders in the rows in the request.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2018-03-19
@pashaa

arrow_keys($data[0])

/* 
$data = [
0 => ['id' => ..., 'customer_id' => ...],
1 => ['id' => ..., 'customer_id' => ...]
...
]
*/
$sql = $db->queryBuilder->batchInsert(self::$table, arrow_keys($data[0]), $data);

Also, $data can be an array of models to insert, in which case arrow_keys($data[0]->attributes) should be written instead of arrow_keys($data[0]).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question