'Set Record Count before $this->setCollection($collection);
How we can set count of collection size before setting for the grid.
Main Sales collection:
$collection=Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('created_at', array('gteq' =>$fromDate))
->addAttributeToFilter('created_at', array('lteq' =>$toDate))
->addAttributeToFilter('status',array('eq'=>Mage_Sales_Model_Order::STATE_COMPLETE));
Had join on that Collection:
$Table = Mage::getModel('amorderattr/attribute')->getResource()->getTable('amorderattr/order_attribute');
$collection->getSelect()->joinLeft(array('custom_attributes' => $amastyTable), 'main_table.entity_id = custom_attributes.order_id',array('is_first_order'));
Another Collection loaded:
$CustomersCollection = Mage::getModel('customer/customer')->getCollection();
$newCustomersCollection = $CustomersCollection
->addAttributeToFilter('created_at',array('from'=>$fromDate,'to'=>$toDate));
if(strlen($storeId)==1)
$newCustomersCollection->addFieldToFilter('website_id',array('eq'=>Mage::getModel('core/store')->load($storeId)->getWebsiteId()));
else if($storeArr){
$newCustomersCollection->addFieldToFilter('website_id',array('eq'=>Mage::getModel('core/store')->load($storeArr[0])->getWebsiteId()));
}
$newCustomers = count($newCustomersCollection);
time periods:
$frm = date("d M Y", strtotime($filterData->getData('from')));
$to = date("d M Y", strtotime($filterData->getData('to')));
$period=(string)$frm." - ". (string)$to;
Another Collection:
$totalAccountsCollection = Mage::getModel('customer/customer')->getCollection();
if(strlen($storeId)==1)
$totalAccountsCollection->addFieldToFilter('website_id',array('eq'=>Mage::getModel('core/store')->load($storeId)->getWebsiteId()));
else if($storeArr){
$totalAccountsCollection->addFieldToFilter('website_id',array('eq'=>Mage::getModel('core/store')->load($storeArr[0])->getWebsiteId()));
}
$totalAccounts=count($totalAccountsCollection);
Again different collection Loaded:
$CustomersCollection1 = Mage::getModel('customer/customer')->getCollection()
->addAttributeToFilter('created_at',array('from'=>$fromDate,'to'=>$toDate));;
$i=0;
foreach ($CustomersCollection1->getData() as $customer) {
$salesOrderCollection2 = Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('customer_id', array('eq' => $customer['entity_id']));
if(!count($salesOrderCollection2->getData())) {
$i++;
}
}
$customer_having_account_but_no_order = $i;
Again some other Info loaded:
/* Total Customer Of USD Website starts */
$USDCustomersCollection = Mage::getModel('customer/customer')->getCollection();
$USDCustomersCollection
->addFieldToFilter('website_id',array('eq'=>1))
->addAttributeToFilter('created_at',array('from'=>$fromDate,'to'=>$toDate));;
$USDAccounts=count($USDCustomersCollection->getData());
/* Total Customer Of USD Website ends */
/* Total Customer Of EURO Website starts */
$EUROCustomersCollection = Mage::getModel('customer/customer')->getCollection();
$EUROCustomersCollection
->addFieldToFilter('website_id',array('eq'=>2))
->addAttributeToFilter('created_at',array('from'=>$fromDate,'to'=>$toDate));;
$EUROAccounts=count($EUROCustomersCollection->getData());
/* Total Customer Of EURO Website starts */
And Finally selecting Columns from it:
$collection->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns('COUNT(*) as total_orders')
->columns('SUM(custom_attributes.is_first_order) as first_order')
->columns('SUM(main_table.customer_is_guest) as is_guest')
->columns('COUNT(*) - (SUM(main_table.customer_is_guest) + SUM(custom_attributes.is_first_order)) as returning_accounts');
$collection->getSelect()->columns(array('new_accounts_created' => new Zend_Db_Expr($newCustomers)))
->columns(array('period' => new Zend_Db_Expr("'".$period."'")))
->columns(array('customer_having_account_but_no_order' => new Zend_Db_Expr($customer_having_account_but_no_order)))
->columns(array('usd_customer_account' => new Zend_Db_Expr($USDAccounts)))
->columns(array('euro_customer_account' => new Zend_Db_Expr($EUROAccounts)))
->columns(array('total_accounts' => new Zend_Db_Expr($totalAccounts)));
And Lastly setting my collection in prepareCollection:
$this->setCollection($collection);
I have to to optimize it and at final setting collection to $this->setCollection() this method it is taking 25 count as result but i get only one row in out put .
Therefore my pagination in of grid showing 25 records instead of 1(row which is getting)
Solution 1:[1]
To fix this, I need to edit one of your core file. As it’s not a good practice to edit core file, I had overridden the core file. Overriding LIB module
Copied Db.php file from magento / lib / Varien / Data / Collection / Db.php Pasted it to local directory so the resultant folder structure would look like this: magento / app / code / local / Varien / Data / Collection / Db.php
I have tried with so many ways Finally I used getSelectCountSql() as well as getSize()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Inzamam Waikar |

