Magento:过滤 getCollection 不起作用

发布于 2024-10-17 18:55:14 字数 618 浏览 2 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(3

你是暖光i 2024-10-24 18:55:14

group 是一个 SQL 关键字。如果 group 也是一个属性名称,您需要以某种方式对其进行转义。尝试使用反引号(Esc 下面通常未使用的键)。

$banner = Mage::getModel('banner/banner')
    ->getCollection()
    ->addFieldToFilter('`group`', 'search_group');

group is an SQL keyword. If group is also an attribute name you'll need to escape it somehow. Try using backticks (the typically unused key below Esc).

$banner = Mage::getModel('banner/banner')
    ->getCollection()
    ->addFieldToFilter('`group`', 'search_group');
旧故 2024-10-24 18:55:14

您误解了错误文本。

 to use near 'group = 'search_group')' 

外部引号是阻止某些内容作为代码的错误消息方式。这可能会更清楚

 to use near [group = 'search_group')]

最好总是查看您的集合正在使用的选择(假设这里是一个非eav集合,给定您的模块创建器样式类别名)并尝试直接在您的MySQL客户端(PHPMyAdmin,命令行应用程序)中运行、查询分析器、Sequel Pro 等)

header('Content-Type: text/plain');
echo (string) $widget->getSelect();
echo "\n";
var_dump ( (string) $widget->getSelect());
Mage::Log((string) $widget->getSelect());
exit;

在上下文中查看整个查询通常可以更轻松地发现错误。

You're misinterpreting the error text.

 to use near 'group = 'search_group')' 

The outer quotes are the error messages way of blocking something off as code. This probably would have been clearer

 to use near [group = 'search_group')]

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

header('Content-Type: text/plain');
echo (string) $widget->getSelect();
echo "\n";
var_dump ( (string) $widget->getSelect());
Mage::Log((string) $widget->getSelect());
exit;

Seeing the entire query in context usually makes spotting an error easier.

轻许诺言 2024-10-24 18:55:14

一种方法是:-

在集合类中编写以下函数:-

public function setGroupBy($group)
{
    $this->getSelect()->group($group);
    return $this;
}

然后您可以像这样使用它:-

$banner = Mage::getModel('banner/banner')->getCollection()->setGroupBy('search_group');

希望这会有所帮助。

One way will be:-

Write the following function in your collection class:-

public function setGroupBy($group)
{
    $this->getSelect()->group($group);
    return $this;
}

Then you can use it like:-

$banner = Mage::getModel('banner/banner')->getCollection()->setGroupBy('search_group');

Hope this helps.

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