Question

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.

Was it helpful?

Solution

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

OTHER TIPS

(Again I can't comment due to my reputation)

I think it's because ? is used as a placeholder in PDO which Magento has abstracted into their model structure. Have you tried escaping it? Something like \?.

Another thing i've noticed and i'm not entirely sure if this is even relevant but theres a method ->quoteInto('AND content = ?', $urlField); although I've not used it.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top