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

Magento 2使用getSelect()->where()无法过滤数据库数据求助

解决Magento 2.2.1后台网格筛选特定Agent ID无数据的问题

嘿,卡几个小时确实太闹心了!我帮你分析下代码里的问题,然后给你修正方案:

问题出在哪?

看你的代码,有几个关键的地方拖了后腿:

  1. 管理员ID获取方式不对:你用$this->_registry->registry('admin_session_id')拿当前管理员ID,这不是Magento官方推荐的方式,而且这个registry键大概率不存在,导致你的$givenAgentId一直是0,筛选条件自然没效果。
  2. 循环逻辑冗余且可能错误:你遍历amasty_perm_dealer全表数据,然后判断user_id等于当前管理员ID时,把givenAgentId设为管理员ID——这逻辑很奇怪!如果你的agent_id是关联经销商的ID,那你应该从这个表取对应的agent_id,而不是直接把管理员ID当agent_id用吧?
  3. Collection处理顺序混乱:父类构造函数已经初始化了$this->collection,但你在else分支又重新创建实例;而且如果givenAgentId不为0时,直接操作getSelect加条件,可能collection已经被加载了,条件根本没生效。
  4. 直接使用ObjectManager:这违反了Magento的依赖注入原则,代码容易出问题,还不好维护。

修正后的代码

我把这些问题都改掉了,你可以直接替换试试(注意根据你的实际表结构调整字段名):

<?php
namespace vendor\plugin\Ui\Component\Listing\DataProviders\Atisstats\Coupons;

use vendor\plugin\Model\ResourceModel\Coupons\CollectionFactory;
use Magento\Backend\Model\Auth\Session as AdminSession;
use Magento\Framework\App\ResourceConnection;

class Listing extends \Magento\Ui\DataProvider\AbstractDataProvider
{
    protected $adminSession;
    protected $resourceConnection;
    protected $givenAgentId = 0;

    public function __construct(
        AdminSession $adminSession,
        ResourceConnection $resourceConnection,
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
        $this->adminSession = $adminSession;
        $this->resourceConnection = $resourceConnection;
        // 先初始化基础collection
        $this->collection = $collectionFactory->create();

        // 正确获取当前登录的管理员ID
        $currentAdminId = $this->adminSession->getUser()->getId();
        
        // 查询amasty_perm_dealer表,拿到当前管理员对应的agent_id(根据你的表结构调整字段)
        $connection = $this->resourceConnection->getConnection();
        $select = $connection->select()
            ->from(
                ['amasty_perm_dealer'],
                ['agent_id'] // 假设你的表有这个字段,存储对应代理ID
            )
            ->where('user_id = ?', $currentAdminId); // 假设user_id是关联管理员ID的字段

        $agentData = $connection->fetchRow($select);
        if (!empty($agentData['agent_id'])) {
            $this->givenAgentId = intval($agentData['agent_id']);
            // 用Magento推荐的方式加筛选条件,比直接操作getSelect更靠谱
            $this->collection->addFieldToFilter('agent_id', $this->givenAgentId);
        }
    }
}

额外说明

  • 为啥硬写数值有用? 因为你之前的$givenAgentId根本没被正确赋值(要么registry没拿到ID,要么循环没命中),导致筛选条件是agent_id = 0,自然没数据;硬写4的时候条件正确,所以能查到。
  • 用addFieldToFilter替代getSelect:这是Magento的规范用法,能避免collection提前加载导致条件不生效的问题,还更安全。
  • 依赖注入替代ObjectManager:这样代码更稳定,也符合Magento的开发标准,以后维护起来也方便。

如果还是有问题,你可以去var/log/system.log里看看有没有报错信息,能更快定位问题~

内容的提问来源于stack exchange,提问作者Shpresim Ndrukaj

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:57:47