Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

如何在Magento 2中加入2个表

使用Magento 2项目时,通常需要在Magento 2中联接2个表。为了使您轻松执行此操作, 建议使用主题在Magento 2中的 2个表之间联接数据。在本主题中,我将指导您如何获取使用指定付款方式(例如“检查汇票”。

Magento 2中2个表之间的联接数据概述

步骤1:设定Collection类别

第一步,您将形成Collection类,该类将扩展 \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection

在Collection类中,您需要了解两个参数:

  • 第一个是您的模块名称
  • 第二个是Magento销售模块中的销售订单资源模型
 protected function _construct()
    {
        $this->_init('Mageplaza\HelloWorld\Model\YourModel', 'Magento\Sales\Model\ResourceModel\Order');
    }

第2步:设置您自己的函数以获取数据

使用代码脚本设置自己的功能,然后根据需要获取数据。

protected function filterOrder($payment_method)
{
    $this->sales_order_table = "main_table";
    $this->sales_order_payment_table = $this->getTable("sales_order_payment");
    $this->getSelect()
        ->join(array('payment' =>$this->sales_order_payment_table), $this->sales_order_table . '.entity_id= payment.parent_id',
        array('payment_method' => 'payment.method',
            'order_id' => $this->sales_order_table.'.entity_id'
        )
    );
    $this->getSelect()->where("payment_method=".$payment_method);
}

步骤3:获取集合并调用上面的filterOrder函数

在模型中,您只需要获取集合并调用filterOrder上面的函数。

$collection = $this->YourCollectionFactory->create();

$collection->filterOrder("checkmo");

foreach ($collection as $item) {
    //do what you want with the data here.
}

步骤4:储存

最后,保存所有内容以完成Magento 2中 2个表之间的联接数据。

这就是给你的。感谢您的阅读!

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论