简体   繁体   English

Magento 2产品集合显示查询

[英]Magento 2 Product Collection displays query

I am having this weird issue, Can someone please point out what i am doing wrong. 我遇到这个奇怪的问题,有人可以指出我做错了什么吗?

I want to display the products in a category related to position added in another table, with following columns 我想在与其他表中添加的位置相关的类别中显示产品,并在以下各列

id, category_id, product_id, position

In my module i extended the \\Magento\\Catalog\\Block\\Product\\ListProduct and \\Magento\\Catalog\\Block\\Product\\ProductList\\Toolbar 在我的模块中,我扩展了\\ Magento \\ Catalog \\ Block \\ Product \\ ListProduct和\\ Magento \\ Catalog \\ Block \\ Product \\ ProductList \\ Toolbar

In the ListProduct file, i override _getProductCollection method and add the following 在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']
            );

And in the Toolbar i override setCollection method 在工具栏中,我重写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;
        }

I get the correct result, but FULL query gets displayed on all catalog pages. 我得到正确的结果,但是FULL查询显示在所有目录页面上。 I have checked everywhere in my code. 我已经在代码中检查了所有地方。 i do not print the query anywhere. 我不在任何地方打印查询。 The best part is if i change 最好的部分是如果我改变

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;

TO

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

In the Toolbar class, removing 'rs' from order function. 在工具栏类中,从订单功能中删除“ rs”。 The order gets reverted to magento default position. 该订单将还原为magento默认位置。 but the query does not get displayed anymore 但查询不再显示

Any Idea ? 任何想法 ?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM