1

While trying to sort product collection by custom attribute, I am facing weird issue. That is, some products are repeated in back pages.

If I print collection query like

$_productCollection->getSelect()->__toString();

and run it directly on mysql, then no product gets repeated.

Debugging indicates - either my custom attribute sorting has problem - because sorting products by name seems fine on listing page.

or magento collection is prematurely executed/modified.

Appreciate your help.

3 Answers 3

2

the product collection extends the Mage_Eav_Model_Entity_Collection_Abstract class. If you try to print the select before loading the collection you may get different results compared to calling it after the load is executed. Take a look at the load method from the class I mentioned.
The filters and sort order (and other things) are added to the select object when you call load. See the call made to _renderFilters and _renderOrders. So if you get the select before you load the collection you don't have any filters applied and no sort order set. Maybe that's why you are getting different results.

2

KnowYourGento, As magento have OOPS concept and MVC logic.

magento have an advance feature Event/Observer By using this is change the collection.So

$_productCollection->getSelect()->__toString() using the code you can get different result.

Magento fetch data using resourcemodel and using Varien_Data_Collection Class convert the collection to object.Varien_Object and Varien_Collection are the parent/super class for most of the Magento Models and Collections respectively.

Main Point is data changes on depends event/Observer and filters

See at Alam Strom Blog

1

I am not able to find exact cause of the issue..why results defer. Whether it is collection preloading issue or some other filteration modifies the magento results compare to sql query result.

But, for those who are in quest of resolving this issue for product duplication on listing page, should try solution mentioned in below like:

Product List - Sometimes show duplicate products

creating observer to order by entity_id worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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