【问题标题】:Magento 2 Product Collection displays queryMagento 2 Product Collection 显示查询
【发布时间】:2017-02-13 17:43:48
【问题描述】:

我遇到了这个奇怪的问题,谁能指出我做错了什么。

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

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

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

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 类中,从 order 函数中删除 'rs'。订单恢复为 magento 默认位置。但查询不再显示

有什么想法吗?

【问题讨论】:

    标签: php mysql magento product magento2


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 2019-12-14
      • 1970-01-01
      相关资源
      最近更新 更多