php - Magento 2 产品集合显示查询

标签 php mysql magento product magento2

我遇到了这个奇怪的问题,有人可以指出我做错了什么吗?

我想显示与另一个表中添加的位置相关的类别中的产品,并带有以下列

id, category_id, product_id, position

在我的模块中,我扩展了\Magento\Catalog\Block\Product\ListProduct 和\Magento\Catalog\Block\Product\ProductList\Toolbar

在 ListProduct 文件中,我重写 _getProductCollection 方法并添加以下内容

$joinConditions = array();
            $joinConditions[] = 'e.entity_id = rs.product_id';
            $joinConditions[] = 'rs.category_id = ' . $category->getId();
            $this->_productCollection->getSelect()->joinLeft(
                    ['rs' => 'my_new_table'], implode(' AND ', $joinConditions), ['position']
            );

在工具栏中我重写 setCollection 方法

switch ($this->getCurrentOrder())
        {
            case 'position':
                if ($this->getCurrentDirection() == 'desc')
                {
                    $this->_collection
                            ->getSelect()
                            ->order('rs.position DESC');
                } elseif ($this->getCurrentDirection() == 'asc')
                {
                    $this->_collection
                            ->getSelect()
                            ->order('rs.position ASC');
                }
                break;

            default:

                if ($this->getCurrentOrder())
                {
                    $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
                }
                break;
        }

我得到了正确的结果,但完整查询显示在所有目录页面上。我已经检查了代码中的所有地方。我不会在任何地方打印查询。最好的部分是如果我改变

case 'position':
                    if ($this->getCurrentDirection() == 'desc')
                    {
                        $this->_collection
                                ->getSelect()
                                ->order('rs.position DESC');
                    } elseif ($this->getCurrentDirection() == 'asc')
                    {
                        $this->_collection
                                ->getSelect()
                                ->order('rs.position ASC');
                    }
                    break;

case 'position':
                    if ($this->getCurrentDirection() == 'desc')
                    {
                        $this->_collection
                                ->getSelect()
                                ->order('position DESC');
                    } elseif ($this->getCurrentDirection() == 'asc')
                    {
                        $this->_collection
                                ->getSelect()
                                ->order('position ASC');
                    }
                    break;

在 Toolbar 类中,从订单函数中删除“rs”。订单将恢复到 magento 默认位置。但查询不再显示

有什么想法吗?

最佳答案

我认为,您应该从中删除 ->getSelect() 并再次检查。

关于php - Magento 2 产品集合显示查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42210215/

相关文章:

php - 当脚本从 Controller 发送到 jquery ajax 时,z-index 不起作用

php - 获取正确的http状态码php

mysql - 如何用字符串+ColY数据+字符串更新所有ColX

PHP 行执行(MySQL 命令)两次(或者在注释掉后根本不执行)

magento - 在 Magento 中禁用系统 crontab 作业

php - 如何在不提示下载的情况下在浏览器中显示以字节形式返回的图像?

php carbon 检查现在是否在两次之间(晚上 10 点到早上 8 点)

php - 使用mysqli将数据插入数据库时​​,出现SQL语法错误

Magento - 如何获取 optionsarray 中所有允许国家/地区的列表?

mysql - Magento MySQL 数据库死锁