Tell me more ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

I have heard many times that they both are same. But I am facing a weird issue, in the product collection of CatalogSearch module, count() is returning correct product count while getSize() is returning zero.

So, basically this is what I am getting:

$collection->count(); //correct count
$collection->getSize(); //0

But I want the getSize() to have correct count as it decides whether to show pagination and products in the search page or not. I am using Inner Join, Left Join and Where condition only in the collection to be more specific.

Any ideas why I am getting this weird issue?

Thanks

UPDATE:

My previous question, How to clone the collection in Magento? I wanted to perform two different operations on one collection. First collection shows correct getSize(), but then if the getSize() is zero, I removed the WHERE clause and gave new WHERE condition. After this I am getting correct raw SQL what I expected, and running it in MySQL also gives correct set of records, but only getSize() on the collection is giving zero count.

So basically I may need to reload the collection, as getSize() is taking old count. Makes sense?

share|improve this question

2 Answers

up vote 4 down vote accepted

Most (if not all) the collections extend Varien_Data_Collection_Db. Here are the 2 methods from this class

public function getSize()
    {
        if (is_null($this->_totalRecords)) {
            $sql = $this->getSelectCountSql();
            $this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
        }
        return intval($this->_totalRecords);
    } 
public function count() //inherited from Varien_Data_Collection
{
    $this->load();
    return count($this->_items);
}

There is a difference. For getSize() the collection is not loaded. For count() it is. Usually collection models use the same getSize() method as above and only override getSelectCountSql().
In getSelectCountSql() the limit is reset in order to get the total number of records available for the set filters (where statement). See how the getSelectCountSql() works

public function getSelectCountSql()
    {
        $this->_renderFilters();

        $countSelect = clone $this->getSelect();
        $countSelect->reset(Zend_Db_Select::ORDER);
        $countSelect->reset(Zend_Db_Select::LIMIT_COUNT);
        $countSelect->reset(Zend_Db_Select::LIMIT_OFFSET);
        $countSelect->reset(Zend_Db_Select::COLUMNS);

        $countSelect->columns('COUNT(*)');

        return $countSelect;
    } 
share|improve this answer
Great! So what should be my next step to reload the collection so that it can get correct getSize()? Thanks! – MagenTools May 23 at 12:04
I honestly don't know why do you get this result. In the CatalogSearch module there is nothing that overrides getSize() or getSelectCountSql(). It should work by default, unless you added some custom code. Can you post the way you build the collection? – Marius May 23 at 12:08
updated the question. – MagenTools May 23 at 12:14
There is no way to reset _totalRecords. You can try to clone the collection before calling getSize() on the original collection. Maybe that will work. – Marius May 23 at 12:19
You're right! Thanks for your help. – MagenTools May 23 at 13:20

Be careful. This is correct, but the methods are overwritten in Varien_Data_Collection_Db as described by Marius

Just have a look into

// \Varien_Data_Collection::getSize
public function getSize()
{
    $this->load();
    if (is_null($this->_totalRecords)) {
        $this->_totalRecords = count($this->getItems());
    }
    return intval($this->_totalRecords);
}

// \Varien_Data_Collection::count
public function count()
{
    $this->load();
    return count($this->_items);
}

So it should on this low level be the same. Both methods load the collection and count the items.

UPDATE

Oh I see a problem: getSize() caches the _totalRecords, this means it is not recalculated. Check where _totalRecords is set?

share|improve this answer
Yes I have looked at it, but can't figure it out why both are generating different counts for the same collection? Any ideas how to reload collection or something to get correct count in getSize()? – MagenTools May 23 at 11:52
updated the entry – Fabian Blechschmidt May 23 at 11:54
getSize() does not load the collection for records that come from the data base. Not unless you override the method and tell it to load the collection. – Marius May 23 at 11:58
Damn it, dug too deep :-) Thanks Marius. – Fabian Blechschmidt May 23 at 12:00
_totalRecords is protected so can't call it in my custom file with collection. echo count($collection->load()->getItems()); gives correct count, but again I want getSize() to work. – MagenTools May 23 at 12:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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