I’m trying to add some items to a custom created collection. Reading Alan Storm varien data collection and this SO question I got to a couple of different options.
Here is one option using Varien_Data_Collection:
$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);
}
}
}
This code throws the following error:
Item (Dts_Banners_Model_Bannersadmin) with the same id "10" already exist
And here is another variation of the code, using my custom collection from a model I currently use:
$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);
}
}
}
But this one shows the following error (not showing everything, is way to long):
Invalid method Dts_Banners_Model_Bannersadmin::addItem(Array
(
[0] => Dts_Banners_Model_Bannersadmin Object
(
[_eventPrefix:protected] => core_abstract
[_eventObject:protected] => object
[_resourceName:protected] => banners/bannersadmin
[_resource:protected] =>
[_resourceCollectionName:protected] => banners/bannersadmin_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
.......
What I’m doing wrong?
Problem #1: Items being added to a
Varien_Data_Collectioninstance need to have distinct IDs in the context of the collection.Problem #2:
Dts_Banners_Model_Bannersadminis not an instance ofVarien_Data_Collection.