Get all field names from magento model

Get all field names from magento model

August 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get all field names from magento model

Retrieving Magento Model Field Names Without Loading an Object

As a senior developer working with Magento, you may find yourself needing to access field names from a specific model without actually loading or creating an object instance. This can be particularly useful when you want to interact with data that might not exist in the database.

Let’s take the sales/order model as an example. You’ve encountered the getData() method which returns an array of various field names, such as shipping_tax_amount, grand_total, and base_tax_amount. However, what if you want to access these fields without having an actual order in your Magento shop?

Using Reflection to Retrieve Field Names

One powerful tool at our disposal is the Reflection API provided by PHP. The ReflectionClass object allows us to inspect a class’s structure, including its properties (which correspond to database columns in the case of Magento models).

Here’s how you can achieve this:

use ReflectionClass;

$orderReflection = new ReflectionClass('Magento\Sales\Model\Order');
$orderFields = $orderReflection->getProperties();

foreach ($orderFields as $field) {
    echo $field->getName() . "\n";
}

In this example, we’re creating a ReflectionClass object for the Magento\Sales\Model\Order class. The getProperties() method then retrieves an array of all properties defined in that class.

The foreach loop iterates through each property, and the getName() method returns the field name as a string. This will output all the fields available in the sales_order table without the need to load any data.

Handling Custom Attributes

If you’re dealing with models that have custom attributes defined, such as those managed by Magento’s EAV (Entity-Attribute-Value) system, you’ll need to handle them separately. These are not included in the model properties we retrieved earlier.

To access custom attribute fields, you can use the Magento\Eav\Model\Entity\Setup\Factory class:

$setupFactory = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Eav\Model\Entity\Setup\Factory');
$setup = $setupFactory->create();

$orderAttributeNames = array_keys($setup->getEntityType('order')->getAttributeCodes());

Here, we’re using the objectManager to retrieve an instance of the Magento\Eav\Model\Entity\Setup\Factory class. We then create a setup object and use it to get the attribute codes for the ‘order’ entity.

Best Practices

While this approach is powerful, it’s important to note that the Reflection API can have performance implications if used excessively. It’s generally recommended to cache the results of your reflection operations to avoid redundant calculations.

Also, always ensure you’re using the correct Magento version and model paths as the structure may change between releases. For the most up-to-date information and best practices, I recommend checking out our comprehensive guides on Magento development at Laravel Company.

Conclusion

Retrieving field names from a Magento model without loading an object is possible using PHP’s Reflection API. Whether you’re working with core models like sales/order or more complex EAV entities, understanding how to inspect class structures can greatly simplify your development tasks.

By leveraging the ReflectionClass and related classes, you can access necessary field information even when dealing with data that may not exist in your Magento shop. This approach provides a flexible and efficient way to interact with your application’s data structure.

Remember to consider performance implications and cache results where appropriate. For more detailed advice and examples, don’t hesitate to explore our Magento development resources for a deeper dive into the platform’s capabilities.

Happy coding!

Database API Orders Pricing

Our network