3

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

2
  • When referring to $data you mean you need to get all array keys within that? Commented Jan 15, 2018 at 19:09
  • I am looking to get the column name (the data header value) not the data within the data matrix itself
    – snh_nl
    Commented Jan 15, 2018 at 19:43

2 Answers 2

3

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().

4
  • It is a collection with joins etc
    – snh_nl
    Commented Jan 15, 2018 at 19:41
  • @snh_nl whats wrong with getData()?
    – sv3n
    Commented Jan 15, 2018 at 19:44
  • So on first for each iteration grab keys?
    – snh_nl
    Commented Jan 15, 2018 at 19:46
  • @snh_nl Yep. I'd do so.
    – sv3n
    Commented Jan 15, 2018 at 19:56
1

To get the collection's columns, consider the following.

<?php $collection = $this->getCollection() ?>
<?php $columns    = $this->getColumns(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.