Magento Forum

   
Methods visibility and overriding classes. 
 
somesid
Sr. Member
 
Total Posts:  83
Joined:  2008-06-20
 

I’ve got a got a situation which is quite puzzling. Now that my website is finished, I’m trying to remove every bit of code which was initially put in the core (baaad I know !), because at the time it was the easiest thing to do (and the way of overriding controllers was not so clear). Well it’s almost done except for this thing.

What we done on our website is that when you click on a category, you get the category page block including an advanced research block (like for example this category page of parquet . Well this is working very well but when we click on “Valider” (launching search) and we don’t choose any criteria we don’t want to throw an exception but to show all the products of the category.

The initial way to do that was to comment the following line in app\code\core\Mage\CatalogSearch\Model\Advanced.php :

if ($allConditions{
            $this
->getProductCollection()->addFieldsToFilter($allConditions);
        
else if (!count($filteredAttributes)) {
            
// Mage::throwException(Mage::helper('catalogsearch')->__('You have to specify at least one search term'));
        
}

Now if I try to override this model with this only change the problem I’m facing is that in the public function addFilters is called :

$this->addSearchCriteria($attribute$value);

which is a private function ! A child class can’t call a parent private function in php5. So I’m pretty much stuck cause if I override this function too, the function itself is modifying a private variable, so I get the same problem.

It brings me to my two questions :
- Any ideas to workaround this problem ? (I wasn’t able to catch the exception to make it disappear).
- Why aren’t those functions protected instead of private ? I thought magento was all about the fact that you’re able to override things and do your custom code.

 
Magento Community Magento Community
Magento Community
Magento Community
 
wdchoi
Jr. Member
 
Total Posts:  20
Joined:  2008-11-17
 

Hi somesid.

Ok, first of all, modifying the core is SIN. You don’t deserve to get an answer for this, and you know it. BUT I’m willing to give you a chance. MWAHAHAHA…

There is a thing I’m wondering about. If you don’t care about modifying the core, why don’t you just change the core for the method you wanna use ? You could change it from private to public… hahaha

However, the real serious question coming to my mind is : why do you want to use addSearchCriteria ? This method is not easy to use, since you first have to load your own attribute object (an attribute code string won’t do it at all). The one you should use is simply addFilters($values) which is public. You have to check of course the structure of the $values array, and that’s all ! Maybe you could tell me more about the specifics of what you want to do.

And magento is all about the fact that you’re able to override the many things that can or even should be overriden, not everything. the addSearchCriteria does not seem to be so.

David

 
Magento Community Magento Community
Magento Community
Magento Community
 
somesid
Sr. Member
 
Total Posts:  83
Joined:  2008-06-20
 

First, thanks for trying to help.

The thing is, that’s what I’m doing, trying to override public function addFilters. I’m using it the usual way in CatalogSearch/controllers/AdvancedController.php only I don’t want it to throw an exception when there is no criteria selected.

So why am I whining about out ?

Well in public function addFilters is called $this->addSearchCriteria($attribute, $value); which is private, so can’t override, so that why I’m ... Well you see.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Well, In an effort to NOT edit core files, you could simple duplicate the content of the original class under a new class name, then rewrite calls to catalogsearch/advanced to point at your new class.  In theory this would subject that new class to the provisions of the OSL, but you would get the ability to change the visability of the addSearchCriteria method as desired AND you would be staying out of the core files.  Keep in mind, this is not the optimal solution.  You still may need to update your copied class on upgrades, but at least you will still be able to upgrade normally.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top