无法使用 getSelect()->where() Magento 2 从数据库中过滤数据
Can't filter data from database using getSelect()->where() Magento 2
我目前正在使用 magento 2.2.1,但遇到了一个奇怪的问题。我正在尝试从数据库中获取一组数据并将其显示在管理网格上。我想获取特定代理 ID 的记录,因此我有一个具有代理 ID 值的变量。当我将此变量作为参数传递给 $this->collection->getSelect()->where('agent_id = ?', $this->givenAgentId);
时,它不会显示任何内容,但如果我用它的值替换 $this->givenAgentId,例如用 4,它会完美地工作!
这是我的 class:
<?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();
}
}
}
我在这里呆了几个小时!
我解决了这个问题!首先是注册表 class 导致了问题,所以我使用了
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resourceUserId = $objectManager->get('\Magento\Backend\Model\Auth\Session');
从会话中获取用户 ID 并在下面使用它来检查用户!由于某种原因,注册表对象正在修改保存当前用户 ID 的变量!
我 post 回答以防万一有人遇到这种问题!
我目前正在使用 magento 2.2.1,但遇到了一个奇怪的问题。我正在尝试从数据库中获取一组数据并将其显示在管理网格上。我想获取特定代理 ID 的记录,因此我有一个具有代理 ID 值的变量。当我将此变量作为参数传递给 $this->collection->getSelect()->where('agent_id = ?', $this->givenAgentId);
时,它不会显示任何内容,但如果我用它的值替换 $this->givenAgentId,例如用 4,它会完美地工作!
这是我的 class:
<?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();
}
}
}
我在这里呆了几个小时!
我解决了这个问题!首先是注册表 class 导致了问题,所以我使用了
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resourceUserId = $objectManager->get('\Magento\Backend\Model\Auth\Session');
从会话中获取用户 ID 并在下面使用它来检查用户!由于某种原因,注册表对象正在修改保存当前用户 ID 的变量! 我 post 回答以防万一有人遇到这种问题!