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; }

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.


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:


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

| improve this answer | |
  • Thank you. Your solution works. I have to replace ? with _ . Can you explain how that works exactly like the search query with ? . That would be helpful. – Franc Jan 8 '14 at 9:46
  • You mean why _ work as part of the like? Or why we need to replace ? with _ in the addFieldToFilter call? – David Manners Jan 8 '14 at 9:54
  • Need to know why '_' work as part of the like '?' – Franc Jan 8 '14 at 11:28

(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.

| improve this answer | |
  • Escaping wont work sadly :( – David Manners Jan 8 '14 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.