count()
loads the collection, therefore it doesn't matter to reload it.
public function count()
{
$this->load();
return count($this->_items);
}
I can't find at this moment any way to reset the
/**
* Loading state flag
*
* @var bool
*/
protected $_isCollectionLoaded;
but I'm sure there was a ($reload = false)
flag somewhere in the collection.
If I'm wrong it is not possible what you want to do, if you don't extend the collection and reset the flag by yourself.
Find something which improves performance. To only count things you should use getSize()
instead of count()
, because it only queries for a COUNT(*)
. But still, I can't find any way to reset $_totalRecords
.
/lib/Varien/Data/Collection/Db.php:208
/**
* Get collection size
*
* @return int
*/
public function getSize()
{
if (is_null($this->_totalRecords)) {
$sql = $this->getSelectCountSql();
$this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
}
return intval($this->_totalRecords);
}
/**
* Get SQL for get record count
*
* @return Varien_Db_Select
*/
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;
}