【发布时间】:2018-07-08 00:46:52
【问题描述】:
我目前正在使用 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();
}
}
}
我已经在这里呆了好几个小时了!
【问题讨论】:
标签: magento grid admin magento2