我遇到了这个奇怪的问题,有人可以指出我做错了什么吗?
我想显示与另一个表中添加的位置相关的类别中的产品,并带有以下列
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/