Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Iterators and Collections
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

In case anyone is doing lots of customization to Magento, and they’re wondering why count($someCollection) is always returning 1, here is the answer.

In code/core/Mage/Eav/Model/Entity/Collection/Abstract.php
add “Countable” to the implements line of the class definition

class Mage_Eav_Model_Entity_Collection_Abstract implements IteratorAggregateCountable

Otherwise, the internal count() function of the collection will never be called and count() will always return 1.  Either that, or use php5’s “iterator_count()” function.

Hope this saves some people some time figuring out why the following code doesn’t work:

if( count($collection) ) {
 
foreach ($collection as $_item{
...
 
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

@Mark: thanks, Varien_Data_Collection and Mage_Eav_Model_Entity_Collection_Abstract were updated to have Countable as interface

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top