我正在为Magento2商店构建管理员(目前在2.1.7上,他们希望使用最新版本直到我们上线然后想要稳定特定版本)。有问题的模块应显示所有现有订单,其中包含一个actionsColumn,其中包含取消,编辑和打开与该订单关联的已购买物料的详细概览的链接。订单明细页面包含一个网格视图,该视图应显示与URL中传递的订单号相关联的所有订单商品。
为了过滤掉与特定订单号无关的订单商品,我已经扩展了\Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
类。除了一个奇怪的警告外,这有效。如果在addFieldToFilter调用中,我将$ ordNum替换为' 10000',它会获取正确的数据。但是,当使用$ ordNum动态调用它时,它根本不返回任何行。尽管尝试了各种类型的铸造和===检查以确保硬编码和动态值之间没有区别。这是Magento的错误吗?我无法弄清楚为什么会出现这种情况。
<?php
class OrderItems extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
{
protected function _initSelect()
{
$this->filterByOrderNum();
parent::_initSelect();
return $this;
}
private function filterByOrderNum()
{
$request = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\App\Request\Http');
$ordNum = $request->getParam('order_num');
$this->addFieldToFilter('order_num', ['eq' => $ordNum]); //if I switch this to hardcoded 10000, this works. With the variable, no dice.
return $this;
}
}
答案 0 :(得分:1)
我只是通过以下提到的步骤对其进行了修复
在控制器的会话中存储参数值
public function execute() {
$this->_catalogSession->setTokenId($this->request->getParam('entity_id'));
$this->_view->loadLayout();
$this->_view->loadLayoutUpdates();
$this->_view->getPage()->getConfig()->getTitle()->set(__('Redeem Token History'));
$this->_view->renderLayout();
}
在dataprovider中使用会话值
$tokensCollection->addFieldToFilter('token_id', ['eq' => $this->_catalogSession->getTokenId()]);
享受:)
答案 1 :(得分:0)
尝试使用此代替getParam语句:
$url = parse_url($request);
$path = explode('/',$url['path']);
$ordNum = $path[3];
为了确保我们在同一页面上,这是完整的代码:
<?php
class OrderItems extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
{
protected function _initSelect()
{
$this->filterByOrderNum();
parent::_initSelect();
return $this;
}
private function filterByOrderNum()
{
$request = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\App\Request\Http');
$url = parse_url($request);
$path = explode('/',$url['path']);
$ordNum = $path[3];
$this->addFieldToFilter('order_num', $ordNum); //if I switch this to hardcoded 10000, this works. With the variable, no dice.
return $this;
}
}
答案 2 :(得分:0)
我们通过以下操作解决了这个问题:
/**
* CcCustompriceProductListingDataProvider constructor.
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param \Magento\Framework\Api\Search\ReportingInterface $reporting
* @param \Magento\Framework\Api\Search\SearchCriteriaBuilder $searchCriteriaBuilder
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
* @param array $meta
* @param array $data
* @throws \Exception
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
ReportingInterface $reporting,
SearchCriteriaBuilder $searchCriteriaBuilder,
RequestInterface $request,
FilterBuilder $filterBuilder,
array $meta = [],
array $data = []
) {
$data['config']['filter_url_params']['product_id'] = $request->getParam('cppc_product_id', 0);
parent::__construct($name, $primaryFieldName, $requestFieldName, $reporting, $searchCriteriaBuilder, $request, $filterBuilder, $meta, $data);
}
您不需要使用任何其他功能。之所以这样,是因为它也使用更新URL进行了更新,并且没有该参数。通过将其添加到数据中,它还将其解析到更新URL中。
您可以在此处看到(家长功能)
/**
* @return void
*/
protected function prepareUpdateUrl()
{
if (!isset($this->data['config']['filter_url_params'])) {
return;
}
foreach ($this->data['config']['filter_url_params'] as $paramName => $paramValue) {
if ('*' == $paramValue) {
$paramValue = $this->request->getParam($paramName);
}
if ($paramValue) {
$this->data['config']['update_url'] = sprintf(
'%s%s/%s/',
$this->data['config']['update_url'],
$paramName,
$paramValue
);
$this->addFilter(
$this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('eq')->create()
);
}
}
}