我正在尝试将订单产品中的SKU放入订单网格并在后端进行过滤。它已经开始工作了,但只针对一个SKU,而不是订单中的所有SKU。我的控制器是这样的:

<?php
namespace Vendor\Module\Model\ResourceModel\Order\Grid;

use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Psr\Log\LoggerInterface as Logger;

class Collection extends OriginalCollection
{
    protected $_authSession;
    public function __construct(
       EntityFactory $entityFactory,
       Logger $logger,
       FetchStrategy $fetchStrategy,
       EventManager $eventManager,
       \Magento\Backend\Model\Auth\Session $authSession
      )
    {
       $this->_authSession = $authSession;
       parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager);
    }

    protected function _renderFiltersBefore() {
        $user = $this->_authSession->getUser();
        $joinTable = $this->getTable('sales_order_item');
        $this->getSelect()->joinLeft($joinTable, 'main_table.entity_id = sales_order_item.order_id', ['sku'])->group('entity_id');
        parent::_renderFiltersBefore();
    }
}

现在我的问题是:是否可以选择左连接中的数据作为组连接?如果是的话,请给我一个提示。
我这样试过
$this->getSelect()->joinLeft($joinTable, 'main_table.entity_id = sales_order_item.order_id', ['sku' => new \Zend_Db_Expr('group_concat(`sales_order_item`.sku SEPARATOR ",")')]);

但没用。

最佳答案

我自己解决的。缺少的是GROUP BY语句,因此我将->group('entity_id')添加到查询中。我还放下了分离器。

$this->getSelect()->joinLeft($joinTable, 'main_table.entity_id = sales_order_item.order_id', ['sku' => new \Zend_Db_Expr('group_concat(`sales_order_item`.sku)')])->group('entity_id');

关于mysql - Magento 2-在左联接中使用group_concat选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53851008/

10-13 00:20