2

How to use query to get result in Magento admin grid & set into collection?

$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query="MY_SQL_QUERY";
$result = $readConnection->fetchAll($query);
$this->setCollection($result);

This is showing error as Call to a member function setPageSize() on a non-object

2

1 Answer 1

3

$result is not an object of magento library class Varien_Data_Collection.

SetPageSize() works whenever a collection is instance of Varien_Data_Collection class.

If you want to use this function then you need convert you that collection ($result ) to Varien_Data_Collection .Then setPageSize() will work.

$collection = new Varien_Db_Collection();
foreach($result as $row){
    $rowObj = new Varien_Object();
    $rowObj->setData($row);
    $collection->addItem($rowObj);
}


$collection->setPageSize($size)

See more detail Check out Alan Storm blog

9
  • 1
    This is a good approach if you explicitly not want to use the Magento methods to fetch data into a grid. Just be aware that setPageSize() and other collection modifiers, such as filters won't do anything if you use a custom query like that. You will have to take care about filters and pagination yourself. Commented Jan 20, 2016 at 8:14
  • @fschmengler,i guess that other way we can do that change Query to resource Model Collection and load this custom collectin using load() function & BeforeLoad().Just like report
    – Amit Bera
    Commented Jan 20, 2016 at 8:19
  • @fschmengler,did you get my point? what Would be your thought on this topc
    – Amit Bera
    Commented Jan 20, 2016 at 8:37
  • 1
    It seems correct but getting error as Fatal error: Class 'Varien_Db_Collection' not found. Commented Jan 20, 2016 at 9:02
  • 2
    Thanks. Used Varien_Data_Collection instead Varien_Db_Collection. Now able to see grid. Commented Jan 20, 2016 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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