代码之家  ›  专栏  ›  技术社区  ›  Shpresim Ndrukaj

无法使用getSelect()->where()Magento 2从数据库中筛选数据

  •  0
  • Shpresim Ndrukaj  · 技术社区  · 7 年前

    我目前正在使用magento 2.2.1,我遇到了一个奇怪的问题。我试图从数据库中获取一组数据,并将其显示在管理网格上。我想记录特定代理ID的记录,以便有一个具有代理ID值的变量。当我将此变量作为参数传递给 $this->collection->getSelect()->where('agent_id = ?', $this->givenAgentId); 它不会显示任何内容,但如果我替换$this->givenAgentId的值,例如4,它工作得很好!

    这是我的班级:

    <?php
    namespace vendor\plugin\Ui\Component\Listing\DataProviders\Atisstats\Coupons;
    
    use \vendor\plugin\Model\ResourceModel\Coupons\CollectionFactory;
    use \Magento\Framework\Registry;
    
    class Listing extends \Magento\Ui\DataProvider\AbstractDataProvider {
    
    protected $_registry;
    
    protected $givenAgentId = 0;
    
    public function __construct(
        Registry $registry,
        $name,
        $primaryFieldName,
        $requestFieldName,
        CollectionFactory $collectionFactory,
        array $meta = [],
        array $data = []
    ) {
        parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
    
        $this->_registry = $registry;
    
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $resource = $objectManager->get('\Magento\Framework\App\ResourceConnection');
        $connection = $resource->getConnection();
        $select = $connection->select()
            ->from(
                ['amasty_perm_dealer'],
                ['user_id']
            );
        $data = $connection->fetchAll($select);
    
        foreach ($data as $dealerId) {
            if ($dealerId['user_id'] == $this->_registry->registry('admin_session_id')) {
                $this->givenAgentId = intval($this->_registry->registry('admin_session_id'));
            }
        }
    
        if ($this->givenAgentId != 0) {
            $this->collection->getSelect()->where('agent_id = ?', $this->givenAgentId);
        } else {
            $this->collection = $collectionFactory->create();
        }
    
    }
    

    }

    我已经在这里呆了几个小时了!

    1 回复  |  直到 7 年前
        1
  •  2
  •   Shpresim Ndrukaj    7 年前

    我解决了这个问题!首先是注册表类导致了这个问题,所以我使用

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resourceUserId = $objectManager->get('\Magento\Backend\Model\Auth\Session');
    

    从会话中获取用户id,并在下面使用它检查用户!出于某种原因,注册表对象正在修改保存当前用户id的变量! 我张贴答案,以防有人遇到这种问题!