Magento loadByAttribute 因问号而失败
即使产品存在,尝试按产品名称加载产品(“什么是测试?”)也会失败。
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定是否可以逃脱。当您使用 Magento 添加属性过滤器(这就是您上面所做的)时,它会使用 Zend 的 quoteInto 方法创建 where 组件,然后将生成的字符串添加到 Zend Select 对象。
然后,当 Zend select 转换为字符串时,结果显示为
The Problem is a full, not paramaterized where is getting added to the select。它是安全的,因为
_getAttributeConditionSql
使用 Zend 的quoteInto
方法,但我很确定这意味着如果你的 where 子句有一个原始的“?”,你就会陷入困境。在那里标记(很高兴被证明是错误的)可能可以通过直接摆弄资源模型的选择来做到这一点,但我不喜欢使用 Magento 这样做。不管怎样,下面的
代码应该可以让您解决这个问题上面的代码创建一个带有 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.
Then, when the Zend select gets converted to a string, it comes out as
The problem is a full, not paramaterized where is getting added to the select. Its safe since
_getAttributeConditionSql
uses Zend'squoteInto
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
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.