0
votes

I'm relatively new to CakePHP and object oriented programming. I have built apps in CakePHP before, but never to this extent, hence the reason I'm asking for help.

I have a large product management database, the user needs to be able to set filter criteria to find the closest product matches.

The products table contains category_id, manufacturer_id, type_id, status_id related to the respective tables.

I have a sidebar with dropdown boxes for each filter, and want all the filters to work together, for instance, if a manufacturer and product type is selected, display those filtered results, and change the status and category dropdowns to only contain options from the already filtered results.

In the past I have accomplished this by using a dynamic query setting conditions based on user input, appending where clauses each time a filter was applied.

so far I've only been able to get individually filtered results using

$this->Product->recursive = 0;
    $mid = (isset($this->params['named']['mid'])) ? $this->params['named']['mid'] : '';
    if(!empty($mid)){
        $this->paginate = array(
            'conditions' => array('Product.manufacturer_id' => $mid),
            'limit' => 10
        );
    }
    $products = $this->paginate('Product');
    $this->set(compact('products'));

where 'mid' is the manufacturer id, and this is manually entering 'mid' in the url

How do I get cumulatively filter results if the user sets category_id, manufacturer_id, type_id, status_id or any combination? And what queries would I use to repopulate the dropdowns?

(Eventually I will use ajax, but for now assume I have 4 dropdown boxes with a submit button)

1

1 Answers

1
votes

The long way would be:

var $conditions = array();

if (isset($this->params['named']['mid']) && !empty($this->params['named']['mid'])){
   $conditions['Product.manufacturer_id'] = $this->params['named']['mid']);
}
if (isset($this->params['named']['catid']) && !empty($this->params['named']['catid'])){
   $conditions['Product.category_id'] = $this->params['named']['catid']);
}
if (isset($this->params['named']['statid']) && !empty($this->params['named']['statid'])){
   $conditions['Product.status_id'] = $this->params['named']['statid']);
}

$this->paginate['conditions'] = $conditions;
$this->paginate['limit'] = '10';
$this->set('products', $this->paginate('Product'));

Once you get the hang of that, you could put all your checks into a loop.