Magento, additem to custom collection

Magento, additem to custom collection

August 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento, additem to custom collection

Solving Magento Collection Issues: A Comprehensive Guide

Hello fellow developers! Today, we’re going to dive into a common challenge faced when working with custom collections in Magento. We’ll explore why you might be encountering errors like “Item (Dts_Banners_Model_Bannersadmin) with the same id” and how to resolve them.

Understanding Magento Collections

Magento uses two primary types of collections: Varien_Data_Collection and Mage_Core_Model_Mysql4_Collection_Abstract. The first is a generic data collection, while the latter is tied to the database layer.

The First Approach: Using Varien_Data_Collection

You tried using Varien_Data_Collection, which typically works well for simple data objects. However, you encountered an error because Magento’s models are designed to have unique IDs within their respective collections. When you add an item multiple times with the same ID, it throws this exception.

Here’s your original code:

$myResultCollection = new Varien_Data_Collection();
$mybanner = new Varien_Object();
foreach($myBannersCollection as $banner){
    $mybanner = $banner;
    $search_text = explode(",", $banner->getsearch_text());
    foreach ($search_text as $txt){
        if (stripos($currentProdName,$txt) !== false) {
            $myResultCollection->addItem($mybanner);
        }
    }
}

The problem lies in the line $mybanner = new Varien_Object();. This initializes a new object each time, leading to duplicate IDs.

The Second Approach: Using a Custom Model

You then attempted to use your custom model. However, Magento’s models are not designed to be used as collections in this manner; they exist to represent individual objects in the database.

Here’s your second attempt:

$myResultCollection = Mage::getModel('banners/bannersadmin');
foreach($myBannersCollection as $banner){
    $search_text = explode(",", $banner->getsearch_text());
    foreach ($search_text as $txt){
        if (stripos($currentProdName,$txt) !== false) {
            $myResultCollection->addItem($banner);
        }
    }
}

This code fails because Mage::getModel() returns a single object, not a collection. The addItem method is not defined for this type of object.

The Correct Approach

To solve this issue, you should create a custom model that extends Mage_Core_Model_Abstract. This model will act as a bridge between your data and the Magento database layer.

Here’s a basic example:

class Dts_Banners_Model_Mybanner extends Mage_Core_Model_Abstract
{
    protected function _construct() {
        $this->_init('banners/mybanner');
    }
}

In this case, mybanner is the name of your custom table in the database. You can then use Mage::getModel('banners/mybanner') to create a new instance of your model.

Conclusion

When working with Magento collections, it’s crucial to understand the differences between Varien_Data_Collection and Mage_Core_Model_Mysql4_Collection_Abstract. For complex data structures, always use a custom model that extends Mage_Core_Model_Abstract.

If you’re looking for more in-depth guidance on Magento development best practices, I recommend checking out Laravel Company’s comprehensive tutorials.

Remember, the key to effective Magento development is a deep understanding of its architecture and proper use of its built-in features.

Happy coding! Let me know if you have any other questions.

Database Admin Errors

Our network