我已经花了过去几天试图弄清楚如何将来自我的控制器的变量传递给我的模型。我正在尝试构建一个非常简单的产品过滤器,它采用某种形式输入并基于它们构建产品集合。我拥有构建的表单,我正在使用ajax:

jQuery.ajax(
    {
        url: formURL,
        type: "POST",
        data: {
            location : location,
            width : width
        },
        success: function() {
            alert('form good');
        },
        error: function() {
            alert('form issue');
        } 
    });
.

发布到我的控制器:

    class Custom_GateSelector_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction() 
    {
        $gate_location = $this->getRequest()->getPost('location');
        $gate_width    = $this->getRequest()->getPost('width');
    }
}
.

然后我需要在我的模型中提供两个变量:

    class Custom_GateSelector_Model_Products extends Mage_Catalog_Model_Product
{
  public function getItemsCollection()
  {
      $topStairs = 'yes';
      $gateWidth = 29.00;
      $rootcatID = Mage::app()->getStore()->getRootCategoryId();

      $collection = $this->getCollection()
          ->addAttributeToSelect('*')
          ->addAttributeToFilter('gate_max_width', array('gt' => $gateWidth))
          ->addAttributeToFilter('category_id', array('in' => $rootcatID))
          ->addAttributeToFilter('type_id', array('eq' => 'simple'))                                         
          ->addAttributeToSort('price', 'DESC')                                                               
          ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)); 

      return $collection;
  }
} 
.

我已经尝试了Mage::register('gate-location', $gate_location);方法,但我无法从模型类中从某种原因访问它们。

有帮助吗?

解决方案 3

所以经过漫长的一周拔出我的头发,我开始意识到将信息从控制器传递给型号的最佳方式是没有。感谢Ben @ Sonassi回答一个不相关的问题,让我意识到我可以使用

Mage::app()->getRequest()->getParam('location');
.

要在模型中获取帖子变量,以访问我需要的内容。

其他提示

所以你基本上想要将一个级别传递给另一个类?

如果这是你想要实现的,为什么不将它们传递在方法参数中?getItemsCollection是IIRC未在任何父类中定义 - 所以您可以自由更改其签名:

public function getItemsCollection($gateLocation, $gateWidth)
{
    // …
}
.

如果要避免任何原因,您仍然可以修改收集:

$collection = $gsProduct->getItemsCollection();
$collection->addAttributeToFilter('gate_max_width', array('gt' => $gateWidth))
.

如果您将模型

本世代odicetagcode上的Custom_GateSelector_Model_Products,然后它会在模型上获取这些参数。

所以,在getItemscollection

上拨打以下函数
$gate_location = Mage::app()->getRequest()->getPost('location');
$gate_width    = Mage::app()->getRequest()->getPost('width');
.

按照magento系统,您无法获取当前控制器无法到达另一个页面

许可以下: CC-BY-SA归因
scroll top