Magento - non eav collection sort order

Magento - non eav collection sort order

September 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - non eav collection sort order

Sorting Non-EAV Collections in Magento

When working with non-EAV collections in Magento, adding sort orders can be a bit more complex compared to EAV collections. While Magento provides convenient methods for sorting EAV data, you’ll need to use standard SQL queries or collection manipulation techniques for non-EAV collections.

Understanding the Challenge

The main issue arises because non-EAV collections don’t have built-in sort orders like their EAV counterparts. Non-EAV models are typically flat tables that lack the flexibility of EAV’s attribute-value structure. Therefore, Magento doesn’t automatically provide sorting options for these collections.

Sorting via SQL Queries

One of the most straightforward approaches is to use raw SQL queries when selecting data from non-EAV collections. You can directly specify the sort order within your query.

$connection = $this->_resource->getConnection('core_read');
$select = $connection->select()
    ->from(['table' => 'your_table'])
    ->order('sort_column ASC');

$dataSet = $connection->fetchAll($select);

In this example, replace 'sort_column' with the actual column name you want to sort by. The ASC keyword sorts the data in ascending order; use DESC for descending order.

Sorting via Collection Manipulation

If you prefer a more Magento-centric approach, you can create your non-EAV collection and then use standard PHP sorting functions.

$collection = Mage::getSingleton('core/resource')->getConnection('core_read')->select()
    ->from(array('table' => 'your_table'));

$dataSet = $connection->fetchAll($select);

usort($dataSet, function($a, $b) {
    return strcmp($a['sort_column'], $b['sort_column']);
});

The usort() function sorts the array based on a custom comparison function. In this case, it uses the strcmp() function to compare the values of 'sort_column' for each item in the $dataSet array.

Conclusion

Sorting non-EAV collections in Magento requires a slightly different approach compared to EAV collections. You have two primary options: using raw SQL queries with the desired sort order or creating a collection and applying standard PHP sorting functions.

Remember, when working with non-EAV collections, it’s crucial to understand your data structure and choose the appropriate sorting method based on your specific needs and performance requirements.

For more advanced techniques in working with Magento collections, I recommend exploring this comprehensive guide: Magento Collection Manipulation Techniques.

Sorting non-EAV collections effectively is a vital skill for any Magento developer. It enables you to extract and manipulate data efficiently from flat tables while still leveraging the power of Magento’s core components.

Let us know your thoughts on this approach or if you have any other queries about working with non-EAV collections in Magento!

Database Orders Errors

Our network