Reputation: 4421
I have made a small admin module that mirrors CMS->Page. The block for my admin page uses the code from app\code\core\Mage\Adminhtml\Block\Catalog\Product\Grid.php
which gives me a grid that I can sort and filter just as I can on the original CMS->Page section.
This is the code in my block for my admin page:
protected function _prepareCollection()
{
$collection = Mage::getModel('cms/page')->getCollection();
$collection->setFirstStoreFlag(true);
$this->setCollection($collection);
return parent::_prepareCollection();
}
Which as I mentioned displays and allows sorting correctly.
However, when I try to modify the data in the rows by amending the above with:
protected function _prepareCollection()
{
$collection = Mage::getModel('cms/page')->getCollection();
$collection->setFirstStoreFlag(true);
foreach ($collection as $order) {
$order->setData( 'title', 'Hello world' );
}
$this->setCollection($collection);
return parent::_prepareCollection();
}
I can no longer sort or filter. Can anyone shed any light on whats happening and if I have the right way of changing row data? thanks
Upvotes: 1
Views: 1455
Reputation: 3694
What's happening: your collection is being loaded. The foreach
language construction triggers the load
method of your collection, and it pulls the data from the db and fill the items
.
Why is your sorting not working? Because the sorting is applied to collection after you've loaded it already. That means that the items are already present in your collection with default sorting order.
How should you do your thing? Right now I don't know what you want to accomplish with that setData
method for each of the collection items. If you're going to use it while creating columns, just add string value in the column.
...->addColumn('title', array('default' => 'Hello world'))
If you would want to pull some more data that is present in the collection, you don't need to load it either, just use collection methods like
addAttributeToSelect
(if it's EAV entities) or addFieldToFilter
.
Upvotes: 2