Better way to add attribute to collection

Better way to add attribute to collection

September 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Better way to add attribute to collection

Refactoring Magento Grid Overrides: A Cleaner Approach

Dear developer,

I understand your concern about the hacky nature of overriding the Mage/Adminhtml/Sales/Order/Grid.php file to include customer EAV attributes in the collection. In this post, I’ll guide you through a cleaner and more maintainable approach using Magento’s built-in capabilities.

The Current Approach: Hacky but Functional

Currently, you’re iterating over each object in the collection and manually loading the corresponding customer model to fetch the campaign_id attribute:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->getCollectionClass());
    foreach ($collection as &$object){
        $customer = Mage::getModel('customer/customer')
            ->setId($object->getCustomerId())
            ->load();
        $object->setCampaignId($customer->getCampaignId());

    }
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

While this works, it’s not the most efficient or maintainable solution. You’re performing multiple database queries and manually populating object properties.

A Better Approach: Using Collection Join

Magento provides a more elegant way to achieve this using collection joins. Let me walk you through an improved version of your code:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->getCollectionClass());
    $joinField = 'customer_id'; // The foreign key field name

    $collection->getSelect()
        ->joinLeft(
            array('customer_table' => $collection->getTable('customer/entity')),
            'main_table.customer_id = customer_table.entity_id',
            array('campaign_id')
        );

    return parent::_prepareCollection();
}

Here’s what I’ve done:

  1. Retrieved the collection as before.
  2. Defined a variable $joinField for the foreign key field name (customer_id in this case).
  3. Called getSelect() on the collection to access its underlying SQL query object.
  4. Used the joinLeft() method to perform a left join with the customer entity table.
  5. Specified the join condition and the fields we want to include from the joined table.

With this approach, Magento automatically handles the database query performance optimizations and joins the necessary tables for you. The EAV attribute values are directly included in the collection without manual object hydration.

Benefits of the Refactored Approach

The refactored version offers several advantages:

  • Performance: By using a single optimized database query, this approach is faster than performing multiple queries.
  • Maintainability: You don’t need to manually handle object properties or deal with customer model instantiation.
  • Scalability: This solution scales better as your data grows, thanks to the efficient Magento core handling.

Conclusion

In conclusion, using collection joins provides a cleaner and more sustainable way to include EAV attributes in your grid collections. It leverages Magento’s built-in capabilities to streamline your code and improve performance.

Remember that maintaining customizations like these requires careful consideration during upgrades to prevent conflicts with Magento core changes. Always ensure you have proper version control and backup mechanisms in place.

I hope this guide helps you refactor your existing solution for a more robust and maintainable implementation.

Happy coding, [Your Name] Laravel Company (https://laravelcompany.com)

Database Admin Orders Customers

Our network