Magento 1.7 Database – How to Bulk-Insert Data

databasemagento-1.7

Is it possible to bulk-insert data into a table using Magento?

I am working on extending the Magento import/export feature for a custom module, and noticed that each row is added one-at-a-time in a while loop. For a CSV containing thousands of rows of data, this could take a VERY long time to insert. Instead, is there a bulk-insert function laying around that takes an array of record data and bulk-inserts them?

Crude example (in reality the row data would be added to the $data array in the while loop):

$data = array(
    array('column1' => 'value', 'column2' => 'value', ...),
    array('column1' => 'value', 'column2' => 'value', ...),
    ...
);

Mage::getModel('module/model')->insert($data,true);

Where the second parameter in insert indicates a bulk-insert operation?

If not, why not? I am aware some PDOs don't have the functionality to do bulk-insert, in which case Magento would default to doing a foreach anyway. However, for PDOs that DO support bulk-insert (such as MySql) it would save a lot of time compared to touching the db for every insert. If this is not already in Magento, how would I go about implementing it? Can I have a link to the Magento feature suggestion page (Google suggests Magento Forum but I would like a second opinion before posting there).

Thanks.

Best Answer

Magento uses Zend_Db at it's core. However, AFAIK there is no way to do bulk inserts using Zend_Db. That said, you could create a method in your collection to accept the input as an array and then iterate over that array saving each entry.

Related Topic