Zend Framework Magento Where 语句 returns 错误
Zend Framework Magento Where statement returns error
这是我在 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);
如果我运行这个查询,它会return一个错误
#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
这是 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%\'')
错误就在这里
(at_name.value 'LIKE
\'%REMAX GREY%\' OR at_name.value LIKE \'%REMAX%\' OR at_name.value LIKE
\'%GREY%\'')
如果我删除 ''
和 \
,它将正常 运行。像这样
(at_name.value LIKE
'%REMAX GREY%' OR at_name.value LIKE '%REMAX%' OR at_name.value LIKE
'%GREY%')
我无法摆脱它,因为 zend 框架是执行 '' 和反斜杠的框架。我该如何处理?
谢谢。
通过 where() 方法添加了额外的 '' 和转义。但是第二个参数是可选的,所以只需将收集的 where 语句附加到条件参数,如下所示:
$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。还在搞乱管理网格中的标记化搜索吗? :) 我很快就忘记了这个。还有更优雅的方法来实现这一点。检查 class Zend_Db_Select
中的 where() 和 _where() 方法
这是我在 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);
如果我运行这个查询,它会return一个错误
#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
这是 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%\'')
错误就在这里
(at_name.value 'LIKE
\'%REMAX GREY%\' OR at_name.value LIKE \'%REMAX%\' OR at_name.value LIKE
\'%GREY%\'')
如果我删除 ''
和 \
,它将正常 运行。像这样
(at_name.value LIKE
'%REMAX GREY%' OR at_name.value LIKE '%REMAX%' OR at_name.value LIKE
'%GREY%')
我无法摆脱它,因为 zend 框架是执行 '' 和反斜杠的框架。我该如何处理?
谢谢。
通过 where() 方法添加了额外的 '' 和转义。但是第二个参数是可选的,所以只需将收集的 where 语句附加到条件参数,如下所示:
$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。还在搞乱管理网格中的标记化搜索吗? :) 我很快就忘记了这个。还有更优雅的方法来实现这一点。检查 class Zend_Db_Select
中的 where() 和 _where() 方法