tags:

views:

55

answers:

1

I'm trying to add some custom filter methods to my magento module. I though it would be simple but I can't figure out why it's not working.

My model which extends the catalog/product class contains this:

public function filterProdType($prod_id)
{
   $this->addAttributeToFilter('attribute_set_id', $prod_id); 

}

Then in my template I have this:

 $collection = Mage::getModel('configurator/product')->getCollection()->addAttributeToSelect('*');


 $collection->filterProdType(50)->addAttributeToFilter('type_id', 'bundle');

 foreach ($collection as $item){
     echo $item->getName() . ', ';
 }

Just to try things out.

But I don't get any results, no error, and it doesn't finish rendering the page (missing footer).

When I do this instead, it works:

 $collection = Mage::getModel('configurator/product')->getCollection()->addAttributeToSelect('*');


 $collection->addAttributeToFilter('attribute_set_id', 50)->addAttributeToFilter('type_id', 'bundle');

 foreach ($collection as $item){
     echo $item->getName() . ', ';
 }

I'm just wondering what I'm missing.

UPDATE:

I didn't realize error reporting was turned off, after turning it on I get the error:

Fatal error: Call to undefined method 
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection::filterProdType() 

I assumed after instantiating $collection using my custom model, it would find my new method.

+1  A: 

When rendering "stops" like that it almost always means Magento encountered a PHP error while trying to include in the phtml file. Take a look at your PHP and/or Apache error logs and see what PHP is complaining about. There's too many variables to troubleshoot things without that information.

If you absolutely can't get at your error logs, delete your entire phtml file and then add code back line by line, block by block, until you can reproduce your problem, and then figure out what's wrong with the last bit you just added.

Alan Storm
I didn't realize error reporting was turned off, after turning it on I get the error:Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection::filterProdType() I assumed after instantiating $collection using my custom model, it would find my new method.Maybe I should put this in my original post. :)
dardub
Your custom method is on the individual model class, but the template is calling it on the collection object.
Alan Storm
Sorry I'm not sure what you mean by individual model class. I must be missing some fundamental understanding. Btw, I'd like to thank you for helping us newbies out with Magento. I've read articles on your blog before, but I think I need to do some re-reading.
dardub
A catalog/product is a single product. It's a Mage_Catalog_Model_Product (or, if you've overridden it, whatever class you've overridden it to). The collection (which you're calling the filterProdType on) is a different object. It's a Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection. You've overridden the base model and added a method, but you're trying to call that method on the collection.
Alan Storm
Okay thanks Alan. It makes sense now. This is turning out more complicated than I expected. So it looks like I'll have to override Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection instead of just extending the class?
dardub
Hard to say without knowing what you're trying to accomplish, but based on what you've said so far I don't see why you need/want to add a method to any of the classes. If you're going to change the phtml file, there's little difference between calling your custom method and calling addAttributeToFilter. You use overrides when you want to change the default behavior of something everywhere. If you're changing the specific behavior of a particular template, just manually filter the collection without going to the trouble of overriding.
Alan Storm
"without going to the trouble of overriding" -- i initially thought I could just group some filters together by just adding a couple of lines of code without having to override anything. but yes, since this is a little more than that, there's not really any point anymore. thanks for all your help Alan.
dardub