You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2:无需Session批量传递选中ID至UI Component表单的正确方式

不使用Session实现批量操作传递客户ID的标准方案

核心思路是将选中的客户ID通过表单隐藏字段传递:打开表单页面时把ID注入到表单初始数据中,提交表单时随表单参数一起发送,完全替代Session的存储方式。

步骤1:修改批量操作跳转控制器(移除Session存储)

修改Vendor/Module/Controller/Adminhtml/Notification/View.php,不再将ID存入Session,而是通过注册器传递给表单:

public function execute()
{
    $selectedIds = $this->getRequest()->getParam('selected');
    $excludedIds = $this->getRequest()->getParam('excluded');

    $customerIds = [];
    // 处理手动选中的客户
    if (!empty($selectedIds)) {
        $customerIds = $selectedIds;
    }
    // 处理全选场景:无选中ID且无排除ID时,获取所有客户ID
    elseif (empty($excludedIds)) {
        $collection = $this->_objectManager->create(\Magento\Customer\Model\ResourceModel\Customer\Collection::class);
        $customerIds = $collection->getAllIds();
    }

    $resultPage = $this->resultPageFactory->create();
    $resultPage->getConfig()->getTitle()->prepend(__('Notification Sender'));

    // 将客户ID注册到全局,供表单DataProvider获取
    $this->_objectManager->get(\Magento\Framework\Registry::class)->register('selected_customer_ids', $customerIds);

    return $resultPage;
}

步骤2:给UI表单添加隐藏字段

在Vendor/Module/view/adminhtml/ui_component/daplcore_notification_view_ui.xml的<fieldset name="general">节点内,新增存储客户ID的隐藏字段:

<field name="customer_ids" formElement="hidden">
    <settings>
        <dataType>text</dataType>
        <dataScope>customer_ids</dataScope>
    </settings>
</field>

步骤3:修改DataProvider注入客户ID

更新Vendor/Module\Model\Ui\NotificationDataProvider.php,从注册器获取客户ID并注入到表单初始数据:

<?php
namespace Vendor\Module\Model\Ui;

use Magento\Framework\Registry;
use Magento\Ui\DataProvider\AbstractDataProvider;

class NotificationDataProvider extends AbstractDataProvider
{
    private $registry;

    public function __construct(
        $name,
        $primaryFieldName,
        $requestFieldName,
        Registry $registry,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->registry = $registry;
    }

    public function getData()
    {
        $data = parent::getData();
        // 将客户ID转为JSON字符串存入隐藏字段
        $customerIds = $this->registry->registry('selected_customer_ids');
        if (!empty($customerIds)) {
            $data['data']['customer_ids'] = json_encode($customerIds);
        }
        return $data;
    }
}

步骤4:提交控制器直接获取客户ID

在Vendor/Module/Controller/Adminhtml/Notification/Send.php中,从表单提交参数里直接读取客户ID:

public function execute()
{
    $resultRedirect = $this->resultRedirectFactory->create();
    $postData = $this->getRequest()->getPostValue();
    
    if (!empty($postData['customer_ids'])) {
        $customerIds = json_decode($postData['customer_ids'], true);
        // 执行给选中客户发送消息的逻辑
        // ...这里编写你的消息发送代码...
        
        $this->messageManager->addSuccessMessage(__('Notification sent successfully.'));
    } else {
        $this->messageManager->addErrorMessage(__('No customers selected.'));
    }
    
    return $resultRedirect->setPath('customer/index/index');
}

内容的提问来源于stack exchange,提问作者Aniruddha Ray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 00:59:57