2 deleted 18 characters in body
source | link

This answer shows up in google for "magento getSize wrong" and similar searches so I would like to add a possible scenario that might be will hopefully be useful to someone

When you have a group statement in your query and you do a

SELECT COUNT(DISTINCT e.entity_id) ... GROUP BY ( at_id_art.value )

Mysql will return a count for EACH of the groups, so Varien_Data_Collection_Db::getSize() will return the wrong answer, this is because this function fetches the first row:

public function getSize()
{
    if (is_null($this->_totalRecords)) {
        $sql = $this->getSelectCountSql();
        $this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
    }
    return intval($this->_totalRecords);
}

When it populates

$this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);

It selects the first row and therefore returns the total of the first group as the total size.

I ended up coming up with this code to count, based on the unique values of the attributes in my query.

$select = clone $collection->getSelect();
$group = $select->getPart(Zend_Db_Select::GROUP);
$select->reset(Zend_Db_Select::GROUP)->reset(Zend_Db_Select::COLUMNS)->columns("COUNT(DISTINCT {$group[0]})");
$totalCount = $collection->getConnection()->fetchOne($select);

This answer shows up in google for "magento getSize wrong" and similar searches so I would like to add a possible scenario that might be will hopefully be useful to someone

When you have a group statement in your query and you do a

SELECT COUNT(DISTINCT e.entity_id) ... GROUP BY ( at_id_art.value )

Mysql will return a count for EACH of the groups, so Varien_Data_Collection_Db::getSize() will return the wrong answer, this is because this function fetches the first row:

public function getSize()
{
    if (is_null($this->_totalRecords)) {
        $sql = $this->getSelectCountSql();
        $this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
    }
    return intval($this->_totalRecords);
}

When it populates

$this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);

It selects the first row and therefore returns the total of the first group as the total size.

I ended up coming up with this code to count, based on the unique values of the attributes in my query.

$select = clone $collection->getSelect();
$group = $select->getPart(Zend_Db_Select::GROUP);
$select->reset(Zend_Db_Select::GROUP)->reset(Zend_Db_Select::COLUMNS)->columns("COUNT(DISTINCT {$group[0]})");
$totalCount = $collection->getConnection()->fetchOne($select);

This answer shows up in google for "magento getSize wrong" and similar searches so I would like to add a possible scenario that might be useful to someone

When you have a group statement in your query and you do a

SELECT COUNT(DISTINCT e.entity_id) ... GROUP BY ( at_id_art.value )

Mysql will return a count for EACH of the groups, so Varien_Data_Collection_Db::getSize() will return the wrong answer, this is because this function fetches the first row:

public function getSize()
{
    if (is_null($this->_totalRecords)) {
        $sql = $this->getSelectCountSql();
        $this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
    }
    return intval($this->_totalRecords);
}

When it populates

$this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);

It selects the first row and therefore returns the total of the first group as the total size.

I ended up coming up with this code to count, based on the unique values of the attributes in my query.

$select = clone $collection->getSelect();
$group = $select->getPart(Zend_Db_Select::GROUP);
$select->reset(Zend_Db_Select::GROUP)->reset(Zend_Db_Select::COLUMNS)->columns("COUNT(DISTINCT {$group[0]})");
$totalCount = $collection->getConnection()->fetchOne($select);
1
source | link

This answer shows up in google for "magento getSize wrong" and similar searches so I would like to add a possible scenario that might be will hopefully be useful to someone

When you have a group statement in your query and you do a

SELECT COUNT(DISTINCT e.entity_id) ... GROUP BY ( at_id_art.value )

Mysql will return a count for EACH of the groups, so Varien_Data_Collection_Db::getSize() will return the wrong answer, this is because this function fetches the first row:

public function getSize()
{
    if (is_null($this->_totalRecords)) {
        $sql = $this->getSelectCountSql();
        $this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);
    }
    return intval($this->_totalRecords);
}

When it populates

$this->_totalRecords = $this->getConnection()->fetchOne($sql, $this->_bindParams);

It selects the first row and therefore returns the total of the first group as the total size.

I ended up coming up with this code to count, based on the unique values of the attributes in my query.

$select = clone $collection->getSelect();
$group = $select->getPart(Zend_Db_Select::GROUP);
$select->reset(Zend_Db_Select::GROUP)->reset(Zend_Db_Select::COLUMNS)->columns("COUNT(DISTINCT {$group[0]})");
$totalCount = $collection->getConnection()->fetchOne($select);