Catalog Search – addFieldToFilter with Like Removing Characters in Query

catalogcatalogsearchcollection-filtering

I have saved a url in the db. The url is: http://localhost/magento/index.php/electronics/cameras.html?cat=25&price=-100

$url = Mage::getSingleton('core/session')->getStateUrl();
$filterstate = Mage::getSingleton('core/session')->getFilterState();
$inc= 1;
  foreach($filterstate as $key=>$value):
    if($inc==1){ $searchurl = $url."?".$key.'='.$value;}
    if($inc!=1){ $searchurl.= '&'.$key.'='.$value; }
   $inc++;
  endforeach;

when i search for the field in the db with my collection

$detect_duplicate = Mage::getResourceModel('savesearch/savesearch_collection');
$detect_duplicate->addFieldToFilter('content', array('like' => $searchurl));
echo $detect_duplicate->getSelect();

The result i got is

SELECT `main_table`.* FROM `savesearch` AS `main_table` WHERE (content LIKE 'http://localhost/magento/index.php/electronics/cameras.html''cat=25&price=-100')

For some reason the ? is emptied. But when i replace it with other characters like * the result was

SELECT `main_table`.* FROM `savesearch` AS `main_table` WHERE (content LIKE 'http://localhost/magento/index.php/electronics/cameras.html*cat=25&price=-100')

Can someone suggest a fix for this. I should able to search for a URL in Db to avoid duplication.

Best Answer

It appears that the problem is when adding attributes to filter the Zend_DbAdapter quoteInto strips out the ?.

I would suggest using an sql wildcard for a single character _ instead of the ?. This will not match exactly ? but will instead match any single character. Note that this could return false results as the two urls that follow would both match:

http://localhost/magento/index.php/electronics/cameras.html?cat=25&price=-100
http://localhost/magento/index.php/electronics/cameras.html*cat=25&price=-100

Have a look at Alan Storm's answer for a bit more detail on the problem: https://stackoverflow.com/questions/3555932/magento-loadbyattribute-fails-on-question-marks

Related Topic