gpt4 book ai didi

mysql - Magento 2 - 在 Left Join 中使用 group_concat 选择

转载 作者:行者123 更新时间:2023-11-29 05:03:10 25 4
gpt4 key购买 nike

我目前正在尝试将订单产品中的 SKU 放入后端的订单网格和过滤器中。它已经在工作,但仅适用于一个 SKU,而不是订单的所有 SKU。我的 Controller 看起来像这样:

<?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();
}
}

现在我的问题是:是否可以选择左侧连接中的数据作为 group_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 ",")')]);

但是没用。

最佳答案

自己解决了。缺少的是 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 - 在 Left Join 中使用 group_concat 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53851008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com