Activerecord Batch Insert

I am wondering if there are any best practices for mass inserting data into DB using ActiveRecord.

I need to be populating database table with few thousand records fetched from array. In production stage I’m planning to move storage to mongodb, this is not the main problem.

I guess creating pure request to DB would be the less resource demanding way, but I like validators and other stuff from ActiveRecords and Models.

For now I have code like this

foreach ($array as $row) {

    $model = new models\Queries();

    $model->attributes = $row;



If there are any ways to optimize this code, I think that should be described in guides.

Hello Dmitry Lukashin,

using active record is an good approach for smaller number of records. if the size of data increases active record apporoach is not the better way to do this for example as you specified that you want to insert hundresds of records in to your table. you can use Command to do this. prepare your query with prepared statements and loop it to insert records.