簡體   English   中英

Magento 2產品集合顯示查詢

[英]Magento 2 Product Collection displays query

我遇到這個奇怪的問題,有人可以指出我做錯了什么嗎?

我想在與其他表中添加的位置相關的類別中顯示產品,並在以下各列

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;

在工具欄類中,從訂單功能中刪除“ rs”。 該訂單將還原為magento默認位置。 但查詢不再顯示

任何想法 ?

我認為,您應該從中刪除-> getSelect()並再次檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM