Magento loadByAttribute在问号上失败 [英] Magento loadByAttribute fails on question marks

查看:114
本文介绍了Magento loadByAttribute在问号上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使产品存在,尝试以其名称(什么是测试?")加载产品也会失败.

Trying to load a product by it's name ("What are Tests?") fails even though the product exists.

$product = Mage::getModel('catalog/product')->loadByAttribute('name', 'What are Tests?');

它可用于其他任何名称.

It works for any other name though.

当Magento最终通过PDO时,?"名称中的名称被解释为参数,并且由于我没有传递任何值,因此结尾查询实际上将在查找什么是测试" ...因此找不到产品?

As Magento ultimately goes through PDO, would the "?" in the name be interpreted as a parameter and as I'm not passing any value for it, the ending query would actually be looking for "What are Tests" ... therefore not finding the product?

如果是这样,我将如何逃脱它?

If so, how would I escape it?

干杯!

推荐答案

我不确定是否可以转义.当您使用Magento添加属性过滤器时(上面的操作),它使用Zend的quoteInto方法创建where组件,然后将结果字符串添加到Zend Select对象.

I'm not sure escaping it is possible. When you add an attribute filter with Magento (which is what you're doing above), it creates the where component using Zend's quoteInto method, and then adds the resulting string to a Zend Select object.

//create a full where clause
//In this case, $conditionSql = IF(_table_name.value_id>0, _table_name.value, _table_name_default.value) = 'What are Tests?'
$conditionSql = $this->_getAttributeConditionSql($attribute, $condition, $joinType);
...
//add that where clause to a Zend select object 
$this->getSelect()->where($conditionSql);

然后,当Zend选择转换为字符串时,它显示为

Then, when the Zend select gets converted to a string, it comes out as

IF(_table_name.value_id>0, _table_name.value, _table_name_default.value) = 'Product 7800'''

问题是一个完整的,没有参数化的地方被添加到选择中.自_getAttributeConditionSql使用Zend的quoteInto方法以来,它是安全的,但是我敢肯定,如果where子句中包含原始的?",则表示您被困住了在那里标记(很高兴被证明是错误的)可以通过直接摆弄资源模型的选择来做到这一点,但是我不喜欢用Magento做到这一点.

The problem is a full, not paramaterized where is getting added to the select. Its safe since _getAttributeConditionSql uses Zend's quoteInto method, but I'm pretty sure it means you're stuck if your where clause has a raw "?" mark in there (happy to be proven wrong on this) It's probably possible to do this by fiddling directly with resource model's select, but I'm not a fan of doing that with Magento.

无论如何,以下内容都可以使您解决此问题

Irrespective of all that, the following should allow you to work around this

$product = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('name',array('like'=>'What are Tests_'))
->getFirstItem();

上面的代码创建一个带有where子句的集合,该子句使用单字符通配符"_"代替?",然后将第一项从顶部移出.这并不理想,但应该可以解决.

The code above create a collection with a where clause that uses the single character wildcard "_" in place of a "?", and then plucks the first item off the top. Not ideal, but it should be a sufficient work around.

这篇关于Magento loadByAttribute在问号上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆