1

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.

3

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))
| improve this answer | |
  • I think this is the proper solution for this problem, since it does not couple the model to a post request. That's why I vote this up. – luemic Jul 7 '15 at 20:07
1

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

| improve this answer | |
  • I'm not sure I understand – Alex Chastain Jul 2 '15 at 21:05
1

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.