Okay, so I'm actually getting somewhere by building a completely different collection...
$category_id = 3;
$collection = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*') // add all attributes - optional
->addAttributeToFilter('type_id', array('eq' => 'configurable'))
->addAttributeToFilter('visibility', array('in' => array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG)))
->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
->addStoreFilter($storeId);
And then fetch the category ID's from the loaded products:-
$cats = $product->getCategoryIds();
And loop through (as @paj suggested in the comments) to get the category data for each:-
foreach ($cats as $category_id) {
$category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id);
}
I'm then able to get what I want for each product like:-
$category->getId()
$category->getName()
Meaning my required mapping works well:-
if ($category->getId()==10) {
$cmap = 'Clothing > Trousers';
}
You can add a link in action column easily. Just follow the standard procedure. Just add a new action in the actions array according to your requirements. As an example, checkout the below code:
$this->addColumn('action',
array(
'header' => Mage::helper('your_module_name')->__('Actions'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('your_module_name')->__('Print'),
'url' => array('base'=> '*/*/print'),
'field' => 'id'
),
array(
'caption' => Mage::helper('brands')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
If you are using collections to fetch data from your models, then its ok, and it does not create any problem related to performance. Also note that magento does not provide any way to pass data from your controllers to a view (except by some nasty ways). So one of the way to keep the MVC pattern is to use helper classes. Also, blocks can be used.
Best Answer
I'm not sure that what you're asking for will solve the problem described. As I read the symptom, my first diagnosis pointed to the following roots:
Supporting the Diagnosis:
$this->_getSelectedProducts()
appends 50K IDs to anIN
clause in your case.parent::_prepareCollection()
instantly loads the collection, but not before applying a limit to the query (see commentary below).Deeper Commentary:
Knowing that the parent widget-grid block [see
parent::_prepareCollection()
] applies a limit to the results returned through the assembled query, the most loaded at once should be no greater than 200. I say this because 200 is the highest default limit given by the toolbar class. So unless you have a modification to your code that would increase the limit beyond 200, I am not certain that the memory exhaustion is caused by 50K assigned products.Therefore, it seems to me that deferred loading won't solve your problem. Ask yourself some questions that might seem unrelated, like:
catalog_product_collection_load_before
orcatalog_product_collection_load_after
?Category Products
grid that would make the select query more complex?I would bet that something else is getting in the way. You could try some brute force guesses by stripping away customizations until the problem goes away. Or else, this is a worthwhile exercise in using a profiler like XDebug to pinpoint when the memory exhaustion occurs.
Deferred Tab Content Loading
If you've followed along so far, you learned that the content is loaded at runtime (or more precisely, layout generation time) for the Category Products tab, and also that Magento offers some unique features for deferring the loading of contents. The easiest solution would be to do the following:
In this rewrite, copy the original
_prepareLayout
method, modifying only the section of code that adds theCategory Products
tab, so that it reads like this:contents
property for a customurl
andclass
property, effectively changing the behavior to load contents from an external source when the tab is selected.Mage_Adminhtml_Catalog_CategoryController::gridAction
. I haven't researched this, but it may have been an abandoned implementation by the original developer that was intended to load the grid exactly as you are looking for today.Give it some thought, a try, and feedback. I'm sure it can be resolved by branching off from one of these suggestions above.