Question

I have spent the last couple of days trying to figure out how to pass variables from my controller to my model. I am trying to build a very simple product filter that takes some form inputs and builds a product collection based on them. I have the form built and I am using AJAX:

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

to post to my controller:

    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');
    }
}

Then I need the two variables there to be available in my model:

    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;
  }
} 

I've tried the Mage::register('gate-location', $gate_location); method but I can't access them in the Model class from some reason.

Was it helpful?

Solution 3

So after a long week of pulling out my hair I came to realize that the best way to pass info from a controller to a model is don't. Thanks to Ben @ Sonassi for answering an unrelated question which made me realize I can use

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

to get the post variable inside the model's action to access what I needed.

OTHER TIPS

So you basically want to pass a variable from one class to another?

If that is what you want to achieve, why not pass them in as method parameters? getItemsCollection is iirc not defined in any parent class – so you are free to change its signature:

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

If you want to avoid that for whatever reason, you can still modify the collection afterwards:

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

If you will the model

Custom_GateSelector_Model_Products on this indexAction() then it would get those parameters on model.

So,call below functions on getItemsCollection

$gate_location = Mage::app()->getRequest()->getPost('location');
$gate_width    = Mage::app()->getRequest()->getPost('width');

As per as magento system ,you cannot get current controller cannot get to another page

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top