0

Is it possible to get a list of the table columns that are related to a model ?

4

It is possible to use the describeTable() method.

So your resource model, you can create a function as the following one, this will return an array of fields for the main table:

public function getFields()
{
    $fields = $this->_getWriteAdapter()->describeTable($this->getMainTable());
    return $fields;
}
| improve this answer | |
  • 1
    I like this method - and great with the self-answer. I was going to suggest Zend_Db_Table's info(Zend_Db_Table_Abstract::COLS) method, but this is great and works within the bounds of pure Magento. – philwinkle Jul 14 '14 at 17:37
  • Thanks, I just dig 10 min in the classes hierarchy of model/resource model and I end up in describeTable() method, to be precise here: Mage_Core_Model_Resource_Db_Abstract::hasDataChanged() ( I usually challenge myself writing question and try to be faster that the community answer :) – Fra Jul 15 '14 at 8:28
1

For who are struggling with this in Magento 2:

In your custom resource model class, add this function:

public function getFields()
{
    $fields = $this->getConnection()->describeTable($this->getMainTable());
    return $fields;
}

And in somewhere you need to get all columns of a table (all fields of object):

$objectModel = $this->objectFactory->create(); // I prefer using dependency injection
$keys = array_keys($objectModel->getResource()->getFields());

P/S: Thank you @Fra for a great self-answer question. It saved a lot of my time.

| improve this answer | |

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.