magento 2-select with group\u concat in left join左连接

bvpmtnay  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(321)

我目前正在尝试将订单产品的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();
    }
}

现在我的问题是:是否可以选择左连接中的数据作为组\u concat?如果是这样的话,请给我一个如何做的提示。我就这样试过

$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 ",")')]);

但没用。

ih99xse1

ih99xse11#

我自己解决的。缺少的是 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');

相关问题