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

  • When referring to $data you mean you need to get all array keys within that? – Christoph Farnleitner Jan 15 '18 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 Jan 15 '18 at 19:43
1

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

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

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, privacy policy and cookie policy

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