首页 > 解决方案 > 如何从 magento 2 中的表 sales_order_item 获取集合数据?

问题描述

我想从表 sales_order_item 获取数据收集并加入其他表。怎么做?我写了这段代码。

$sale_order = $this->_resource->getTableName('sales_order');

        $sale_order_item_table = $this->_resource->getTableName('sales_order_item');

        $catalog_category_product_table = $this->_resource->getTableName('catalog_category_product');

        $catalog_category_entity_text_table = $this->_resource->getTableName('catalog_category_entity_text');

我想创建集合,但我不知道如何创建正确的 _salesOrderItemCollectionFactory $saleOrderItemCollection = $this->_salesOrderItemCollectionFactory->create();

请帮我?

非常感谢,BienHV

标签: magento2

解决方案


我认为您需要使用 getSelect 来创建这样的连接查询,然后使用此查询加载集合。

public function execute()
{
    /* @var \Magento\Sales\Model\ResourceModel\Order\Item\Collection $collection*/
    $collection = $this->orderItemsCollectionFactory->create();
    $joinQuery = $collection->getSelect()
        ->join(['so' =>'sales_order'],'main_table.order_id = so.entity_id','state');
    $result = $collection->load($joinQuery);
    return $result->getData();
}

推荐阅读