Magento:过滤 getCollection 不起作用
我的 getCollection 代码提供了错误的查询字符串(我认为)。
我有一个名为横幅的表,我可以很容易地加载所有记录。当我尝试过滤它时,我收到错误。
这是代码:
$banner = Mage::getModel('banner/banner')->getCollection()->addFieldToFilter('group', array('eq'=>'search_group'));
页面崩溃,我收到此错误:
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group = 'search_group')' at line 1";i:1;s
正如您所看到的,代码似乎弄乱了组后的引号。
'group = 'search_group')'
任何人都可以建议如何解决这个问题吗?
谢谢,
比利
My getCollection code is providing the wrong query string (i think).
I have a table called banner which I can load all records from easy enough. When I try to filter it I'm getting errors.
Here is the code:
$banner = Mage::getModel('banner/banner')->getCollection()->addFieldToFilter('group', array('eq'=>'search_group'));
The page crashes and I get this error:
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group = 'search_group')' at line 1";i:1;s
As you can see it seems like the code is messing up the quotes after group.
'group = 'search_group')'
Can anyone advice on how to fix this?
Thanks,
Billy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
group
是一个 SQL 关键字。如果group
也是一个属性名称,您需要以某种方式对其进行转义。尝试使用反引号(Esc 下面通常未使用的键)。group
is an SQL keyword. Ifgroup
is also an attribute name you'll need to escape it somehow. Try using backticks (the typically unused key below Esc).您误解了错误文本。
外部引号是阻止某些内容作为代码的错误消息方式。这可能会更清楚
最好总是查看您的集合正在使用的选择(假设这里是一个非eav集合,给定您的模块创建器样式类别名)并尝试直接在您的MySQL客户端(PHPMyAdmin,命令行应用程序)中运行、查询分析器、Sequel Pro 等)
在上下文中查看整个查询通常可以更轻松地发现错误。
You're misinterpreting the error text.
The outer quotes are the error messages way of blocking something off as code. This probably would have been clearer
It's always best to look at the select your collection is using (assuming a a non-eav collection here, given your Module Creator style class alias) and try running in directly in your MySQL client (PHPMyAdmin, Command Line app, Query analyzer, Sequel Pro, etc.)
Seeing the entire query in context usually makes spotting an error easier.
一种方法是:-
在集合类中编写以下函数:-
然后您可以像这样使用它:-
希望这会有所帮助。
One way will be:-
Write the following function in your collection class:-
Then you can use it like:-
Hope this helps.