Question

So I'm using the (totally awesome) Cake DC Search plugin. Among other things, I want to have a 'min' and 'max' input, and search for records where 'amount' is within 'min' and 'max', if both are set, or greater-than/less-than min/max if only one is set.

I've got it working - the only problem is that the condition is being included twice in the SQL query. It's pretty obvious why... here's my model code:

public $filterArgs = array(
    'range_min' => array('type' => 'query', 'method' => 'findByAmountRange'),
    'range_max' => array('type' => 'query', 'method' => 'findByAmountRange'),
    // Other rules go here
);


public function findByAmountRange($data = array()) {
    // Returns a condition something like 'Model.amount BETWEEN 1 and 100'
}

So because both the 'range_min' and 'range_max' keys of $filterArgs both use the same method (findByAmountRange), then the condition is being included twice.

So, there are three solutions:

  1. Don't worry about it, let the condition be there twice

  2. Create a instance variable in the model, like findByAmountRangeHasBeenCalled, and set it true the first time the method is called, so we know not to call it again in future

  3. Find a more elegant solution. Does the Search plugin have any way to deal with this? Can you set it to make two fields share a common filterArgs method, so that it only gets called once?

Thanks in advance.

PS - I know the test cases for the Search Plugin include a 'make range condition' example, but that's not what I want - I want two fields, with the user able to input into both of them.

Was it helpful?

Solution

OK, so it doesn't look like I'll get a proper answer. So, I've gone for option 1 - just leaving the duplicate condition in the query. Apparently MySQL will run a query optimiser prior to executing, and remove the duplicate condition, and so it won't affect speed in any significant way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top