How to insert multiple data in array to database via controller in Magento

How to insert multiple data in array to database via controller in Magento

October 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to insert multiple data in array to database via controller in Magento

Inserting Multiple Data into Database via Controller in Magento

The Challenge: Inserting Array Data into Custom Table

You’ve created a custom table called test_category with columns entity_id, category_id, and category_name. You have the category data stored as an array, but you’re unsure how to insert this multi-dimensional array directly into your database via a Magento controller. Let’s tackle this challenge step-by-step.

Understanding Your Data Structure

Your current array structure looks like this:

Array
(
    [category_id] => Array
        (
            [0] => 13361
            [1] => 13697
            [2] => 13755
            [3] => 13760
        )
    [category_name] => Array
        (
            [0] => Baby & Toddler
            [1] => Baby & Toddler - Baby Gear
            [2] => Baby & Toddler - Baby Gear - Backpacks & Carriers
            [3] => Baby & Toddler - Baby Gear - Backpacks & Carriers  - Accessories
        )
)

This array consists of two sub-arrays: category_id and category_name. Each index corresponds to the same position in both arrays.

The Magento Solution: Using Model for Insertion

Magento provides a straightforward way to insert data into your custom table using models. Here’s how you can achieve this:

  1. Create a Custom Model: If you haven’t already, create a model for your test_category table. You can name it something like TestCategory.
class TestCategory extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init('yourmodule/testcategory');
    }
}

Replace 'yourmodule' with the actual module name.

  1. Insert Data in a Loop: In your controller, you can loop through your array data and use the TestCategory model to insert each row into the database.
$array = array(
    'category_id' => array(13361, 13697, 13755, 13760),
    'category_name' => array('Baby & Toddler', 'Baby & Toddler - Baby Gear', 'Baby & Toddler - Baby Gear - Backpacks & Carriers',
                              'Baby & Toddler - Baby Gear - Backpacks & Carriers  - Accessories')
);

$model = Mage::getModel('yourmodule/testcategory');
foreach ($array['category_id'] as $index => $id) {
    $model->setCategoryId($id);
    $model->setCategoryName($array['category_name'][$index]);
    try {
        $model->save();
        Mage::log('Inserted category: ' . $id);
    } catch (Exception $e) {
        Mage::log('Error inserting category ' . $id . ': ' . $e->getMessage());
    }
}
  1. Save the Model: The save() method is used to insert a new row into the database or update an existing one based on the primary key.

Best Practices and Tips

  • Always use transactions when dealing with multiple inserts to ensure data consistency.
  • Handle exceptions gracefully, as shown in the example above.
  • If your array contains many sub-arrays, you might want to look into using Magento’s Mage_Core_Model_Mysql4_Abstract.php for bulk insertion, which can perform better than looping and saving individually.

Conclusion

Inserting multi-dimensional array data directly into a database via a Magento controller is achievable. By following the steps above, you can efficiently insert your category data into the test_category table using the TestCategory model.

Remember to replace 'yourmodule' with the actual name of your module in the example code. This ensures that the model and the controller are correctly linked within your Magento installation.

For more advanced database operations in Magento, I recommend checking out our guide on Mysql4 Models for Better Database Interaction.

Catalog Indexing Database

Our network