Scenario
Suppose say we have an array of data fetched from database.
And we want to merge it with Magento collection object or want to
provide the data as collection object to the view(template) file.
Solution
<?php
$resource = Mage::getModel('core/resource');
$connection = $resource->getConnection('core_read');
$sql = "SELECT * FROM custom_table WHERE some_field = ?";
$rows = $connection->fetchAll($sql, array($someFieldValue));//this row will return an array
$collection = new Varien_Db_Collection();
foreach($rows as $row){
$rowObj = new Varien_Object();
$rowObj->setData($row);
$collection->addItem($rowObj);
}
//now you can get the data using collection way
foreach($collection as $_data){
print_r($_data->getData());
}
Notes:
In order to create a collection object
1> Create an instance of Varien_Db_Collection
$collection = new Varien_Db_Collection();
2> Create an instance of Varien_Object and set the array of data
$rowObj = new Varien_Object();
$rowObj->setData($row);
3> Finally add the Varien_Object to Collection instance
$collection->addItem($rowObj);
Now you can play with the magic getters and setters of collection class.
In order to have depth knowledge on Magento Collection you can refer to the Alan Storm’s article:
Varien Data Collections
来源: http://www.blog.magepsycho.com/how-to-convert-an-array-to-a-collection-object-in-magento/
分享到:
相关推荐
Chapter 6, Creating a Magento 2 Theme, discusses the Magento 2 blank theme and how to use the fallback to create seasonal variations. It also explains how the new theme is set up and where files are ...
Chapter 6, Creating a Magento 2 Theme, discusses the Magento 2 blank theme and how to use the fallback to create seasonal variations. It also explains how the new theme is set up and where files are ...
You’ll start by getting a general understanding of what Magento is, why and how you should use it, and whether it is possible and feasible to migrate from an old web store to Magento 2. As you work ...
magento入门原版书.。。The Definitive Guide to Magento.pdf
Apress.The.Definitive.Guide.to.Magento.Dec.2009.pdf Apress于2009重磅推出的Magento上手书籍,原书地址:http://www.apress.com/book/view/9781430272298
Get a hands-on introduction to custom shipping and payment methods ☆ 出版信息:☆ [作者信息] Branko Ajzele [出版机构] Packt Publishing [出版日期] 2013年09月25日 [图书页数] 128页 [图书语言] 英语...
magento数据结构分析 magento数据字典
magento的SMTP插件magento的SMTP插件magento的SMTP插件
magik shoes magento 模板, magento 1.7 模板
Magento插件开发手册 Magento Extension Developers Guide
深入理解Magento 由精东博客下载下来制作成pdf
主要包括的内容有:magento的常用设置、Magento模板制作教程、Magento商城_目录结构说明、Magento中文教程实用文档、常用的xml的文件的作用以及自定义模块的应用
Athlete Magento主题,兼容Magento 1.9.x, 1.8.x, 1.7.x ,特点:完全自适应(响应式布局),自定义子主题,无限颜色,完全自定义,一键安装演示,小工具化横幅系统,颜色选择,MEGA菜单,多商店支持,自定义产品...
如何给产品描述里的关键字、字词批量添加链接?我想大家一直在的这样的...并支持到Magento1.6.2 说明请查看:http://www.hicoogle.com/magento-gei-chan-pin-miao-shu-guan-jian-zi-pi-liang-lian-jie-cha-jian.html
magento2 developers cookbook, magento 开发手册,magento教程
快速在服务器上复制为另一个站,版本用的是magento,php语言
Magento 的配置系统就像是 Magento 的心脏,支撑着 Magento 的运行。这套 配置系统掌管着几乎所有“module/model/class /template/etc”。它把整个 Magento 系统抽象出来,用一个配置文件来描述。这里的“配置文件”...
两本关于Magento的点子书 希望大家喜欢 Apress.The.Definitive.Guide.to.Magento.Dec Magento 1.3 Theme Design