如何在Magento 2中从索引控制器到集合获取价值

时间:2019-12-20 15:29:06

标签: magento2

我正在尝试从索引控制器获取值。

Controller \ Adminhtml \ Orders \ Index.php:

  public function execute()
  { 
    $storeid = $this->getRequest()->getParam('storeid');
    $this->settings->setStoreid($storeid);
   }

Model \ Settings.php

public function setStoreid($storeid)
{
    $this->storeid = $storeid;
}

public function getStoreid()
{
    return $this->storeid;
}

Model \ ResourceModel \ Order \ Grid \ Collection.php:

   public function _renderFiltersBefore{       
      $storeid = $this->getStoreId();

     // var_dump($storeid)
      // $storeid = 5

      $this->getSelect()->where('store_id = ?', $storeid);
   }

我想将storeid传递给查询Collection。我可以从Index类设置storeid,也可以在Collection类中获取storeid。但是它不会传递给集合。 如果我直接将值设置为storeid,即。 $ storeid = 5,则收集工作正常。请指教。

1 个答案:

答案 0 :(得分:0)

也许尝试使用注册表

/**
  * @var \Magento\Framework\Registry
  */

 protected $_registry;

 /**
 * ...
 * ...
 * @param \Magento\Framework\Registry $registry,
 */
public function __construct(
    ...,
    ...,
    \Magento\Framework\Registry $registry,
    ...
) {
    $this->_registry = $registry;
    ...
    ...
}

 /**
 * Setting custom variable in registry to be used
 *
 */

public function setCustomVariable()
{
     $this->registry->register('custom_var', 'Added Value');
}

/**
 * Retrieving custom variable from registry
 * @return string
 */
public function getCustomVariable()
{
     return $this->registry->registry('custom_var');
}