我正在尝试将一些项目添加到自定义创建的集合中。阅读Alan Storm varien data collection和这个SO question我有几个不同的选择。
这是使用 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);
}
}
}
此代码引发以下错误:
Item (Dts_Banners_Model_Bannersadmin) with the same id "10" already exist
这是代码的另一个变体,使用我当前使用的模型中的自定义集合:
$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);
}
}
}
但是这个显示了以下错误(没有显示所有内容,太长了):
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] =>
.......
我做错了什么?