2017-02-13 98 views
0

我有這個奇怪的問題,有人請指出我做錯了什麼。Magento 2產品集合顯示查詢

我想在另一個表位置,增加,與下面的列

id, category_id, product_id, position 

在我的模塊我延長了\ Magento的\目錄\塊\產品\ ListProduct 和相關的類別,以顯示產品\ Magento的\目錄\塊\產品\產品列表\工具欄

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

TO

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默認位置。但查詢不再顯示

任何想法?

回答

0

我想,你應該從它刪除 - > getSelect()並再次檢查。