我試圖添加一些項目到自定義創建的集合。閱讀Alan Storm varien data collection和這SO question我得到了幾個不同的選擇。Magento,添加到自定義集合
下面是使用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);
}
}
}
但是,這一個顯示以下錯誤(不sh由於一切,是長的方式):
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] =>
.......
我做錯了什麼?
第二個。在Magento中,並不是所有的集合都從基本的Varien_Data_Collection「繼承」? – Yaroslav
所有典型的Magento集合都使用'Varien_Data_Collection';那些通過'Varien_Data_Collection_Db'使用Magento的ORM for DB。 – benmarks
不知道,現在檢查我認識的Alan Storm網站。將嘗試使用它,看看會發生什麼。你推薦什麼,使用'Varien_Data_Collection'或'Varien_Data_Collection_Db'? – Yaroslav