doulu2011 2017-10-30 12:52
浏览 48
已采纳

Zend Framework Magento Where语句返回错误

This is my Query in Magento.

        $where = "LIKE '%".$value."%'";
        foreach($tokens as $token) {
            $where .= " OR at_name.value LIKE '%$token%'";
        }

        $this->getCollection()->getSelect()
             ->joinInner(array('at_name' => 'mgmx_catalog_product_entity_text'), '(at_name.entity_id = at_visibility.entity_id)')
             ->where("at_name.value ?" ,$where);

If I run this Query, it will return an error

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''LIKE \'%REMAX GREY%\' OR at_name.value LIKE \'%REMAX%\' OR at_name.value LIKE \' at line 4

This is the query in Mysql.

SELECT COUNT(DISTINCT e.entity_id) 
FROM `mgmx_catalog_product_entity` AS `e`
INNER JOIN `mgmx_catalog_product_entity_int` AS `at_status` 
ON (`at_status`.`entity_id` = `e`.`entity_id`) AND 
(`at_status`.`attribute_id` = '96') AND (`at_status`.`store_id` = 0)
INNER JOIN `mgmx_catalog_product_entity_int` AS `at_visibility` 
ON (`at_visibility`.`entity_id` = `e`.`entity_id`) AND 
(`at_visibility`.`attribute_id` = '102') AND (`at_visibility`.`store_id` = 0)
INNER JOIN `mgmx_catalog_product_entity_text` AS `at_name` ON (at_name.entity_id = at_visibility.entity_id) WHERE (at_name.value 'LIKE 
\'%REMAX GREY%\' OR at_name.value LIKE \'%REMAX%\' OR at_name.value LIKE 
\'%GREY%\'')

The error is here somewhere

 (at_name.value 'LIKE 
\'%REMAX GREY%\' OR at_name.value LIKE \'%REMAX%\' OR at_name.value LIKE 
\'%GREY%\'')

If I remove the '' and the \ it will run normally. Like so

(at_name.value LIKE 
'%REMAX GREY%' OR at_name.value LIKE '%REMAX%' OR at_name.value LIKE 
'%GREY%')

I can't get rid of it as the zend framework is the one doing the '' and the backslashes. How do i deal with this?

Thanks.

  • 写回答

1条回答 默认 最新

  • dp152153 2017-10-30 13:27
    关注

    Additional '' and escaping were added by method where(). But the second parameter is optional so simply append collected where statement to condition parameter like this:

    $this->getCollection()->getSelect()
                 ->joinInner(array('at_name' => 'mgmx_catalog_product_entity_text'), '(at_name.entity_id = at_visibility.entity_id)')
                 ->where("at_name.value ".$where);
    

    PS. still messing around with tokenized search in admin grid? :) I coded this quickly out of mind. There are even more elegant ways to implement this. check where() and _where() methods in class Zend_Db_Select

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀