Skip to content

Instantly share code, notes, and snippets.

@cornernote
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save cornernote/de040fc19a0dda40fea1 to your computer and use it in GitHub Desktop.

Select an option

Save cornernote/de040fc19a0dda40fea1 to your computer and use it in GitHub Desktop.
Yii2 batchInsert

I am using batchInsert to insert bulk data, but its inserting in the wrong order in php5.4 (php5.5+ is fine).

I found the reason, but dont understand where Yii2 is doing this...

I am looking at the source for batchInsert (https://github.com/yiisoft/yii2/blob/master/framework/db/QueryBuilder.php#L181), and I dont understand how it's doing the unions.. i cant see code for that anywhere

PHP 5.4

INSERT INTO `audit_trail` (`audit_id`, `user_id`, `old_value`, `new_value`, `action`, `model`, `model_id`, `field`, `stamp`) 
SELECT NULL, NULL, '', '4', 'CREATE', 'tests\models\Post', '4', 'id', '2015-06-22 15:02:38' 
UNION SELECT NULL, NULL, '', 'New post title', 'CREATE', 'tests\models\Post', '4', 'title', '2015-06-22 15:02:38' 
UNION SELECT NULL, NULL, '', 'New post body', 'CREATE', 'tests\models\Post', '4', 'body', '2015-06-22 15:02:38'

PHP 5.5+

INSERT INTO `audit_trail` (`audit_id`, `user_id`, `old_value`, `new_value`, `action`, `model`, `model_id`, `field`, `stamp`) VALUES 
(NULL, NULL, '', '4', 'CREATE', 'tests\models\Post', '4', 'id', '2015-06-22 15:02:48'), 
(NULL, NULL, '', 'New post title', 'CREATE', 'tests\models\Post', '4', 'title', '2015-06-22 15:02:48'), 
(NULL, NULL, '', 'New post body', 'CREATE', 'tests\models\Post', '4', 'body', '2015-06-22 15:02:48')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment