Magento loadByAttribute 因问号而失败

发布于 2024-09-16 09:10:32 字数 313 浏览 2 评论 0原文

即使产品存在,尝试按产品名称加载产品(“什么是测试?”)也会失败。

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

但它适用于任何其他名称。

当 Magento 最终通过 PDO 时,“?”会出现吗?名称中的 被解释为参数,并且由于我没有为其传递任何值,因此结束查询实际上会寻找“什么是测试”......因此找不到产品?

如果是这样,我该如何逃避呢?

干杯!

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.

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?

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随波逐流 2024-09-23 09:10:32

我不确定是否可以逃脱。当您使用 Magento 添加属性过滤器(这就是您上面所做的)时,它会使用 Zend 的 quoteInto 方法创建 where 组件,然后将生成的字符串添加到 Zend Select 对象。

//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 select 转换为字符串时,结果显示为

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

The Problem is a full, not paramaterized where is getting added to the select。它是安全的,因为 _getAttributeConditionSql 使用 Zend 的 quoteInto 方法,但我很确定这意味着如果你的 where 子句有一个原始的“?”,你就会陷入困境。在那里标记(很高兴被证明是错误的)可能可以通过直接摆弄资源模型的选择来做到这一点,但我不喜欢使用 Magento 这样做。

不管怎样,下面的

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

代码应该可以让您解决这个问题上面的代码创建一个带有 where 子句的集合,该子句使用单字符通配符“_”代替“?”,然后从顶部取出第一个项目。并不理想,但它应该是一个足够的解决方法。

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

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'''

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();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文