Magento – How to get collection result “column headers” or “column names” in Magento 1.9x

collection;databasemagento-1magento-1.9

How can I get collection result "column headers" or "column names" from query?

Suppose I have $data = Mage::getModel('catalog/product')->getCollection(); and I have some joins with other tables

I can get the data by running foreach on $data giving me the actual output data.

question: but how can I get the "header name" (or "column name") as I would see it when I run the same query in MySQL

enter image description here

Best Answer

For a simple query like SELECT * FROM 'sales_flat_order'

$res    = Mage::getSingleton('core/resource');
$read   = $res->getConnection('core_read');
$table  = $res->getTableName('sales/order');
$header = array_keys($read->describeTable($table));

To get all product attributes from Mage::getModel('catalog/product')->getCollection() just use keys from $item->getData().

Related Topic