Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

A minor update to searchable, which allows you to pass FALSE from your '... #36

Closed
wants to merge 2 commits into from

5 participants

@zeroasterisk

...subquery' [_addCondSubquery()] and 'query' [_addCondQuery()] methods and bypass the addition to the search criteria -- this allows you to break out of passing a search condition based on some 'special case' values or processing... cheap and easy

@zeroasterisk zeroasterisk A minor update to searchable, which allows you to pass FALSE from you…
…r 'subquery' [_addCondSubquery()] and 'query' [_addCondQuery()] methods and bypass the addition to the search criteria -- this allows you to break out of passing a search condition based on some 'special case' values or processing... cheap and easy
c4a6eb5
@dereuromark

I don't think it matches cake's coding standards, though.
if ($subquery !== false) {

Oh? because you'd rather check for === false and return out of the function?

ähm, not exactly, just the spaces around it (just compare it with your code^^)

ah yes -- my mistake -- will correct and resubmit the pull request.

By specification each of query and subquery method should return empty array (not boolean value).

@skie

By specification each of query and subquery method should return empty array (not boolean value).

@skie skie closed this
@zeroasterisk
@skie

No search is not aborted in case we should ignore just one of search conditions. That's main idea that user can specify search step by step. In case if there are no search conditions applied user get all data.

@zeroasterisk

Yes -- I understand... but in the method on my model which is called by _addCondSubquery() there are some logical exceptions, based on input data, which might result in want to not filter the result set at all, even though we've passing in a value to that function (ie: that value is depreciated, but still a possible input due to old URL).

There is no way to abort executing a subquery and filtering the results with it... even if we pass in a subquery which doesn't filter the results ( WHERE id > 0 or something ) it's still extra and unnecessary weight on the database server. I'd be nice (and I think, easy) to verify that there is in fact something to use as a subquery to check... so my subquery method on my Model can choose not to filter the result set.

@skie

In case you want to stop the search process it is possible to throw some LogicException and catch it on controller level.

@zeroasterisk

Yes, but all I want to stop is the subquery part of the search, other parameters may well still be valid and should filter the results.

Like this example, where we are passing one depreciated value to the search parameters, and other valid values:

// controller
$this->passedArgs = array('category' => 'depreciated', 'type' => 'text', 'is_active' => 1);

Obviously, I could filter such a simple example in the controller before passing it to the model, but imagine a situation where we didn't know the category was depreciated in the controller -- only by doing a bit of logic in the model did we know. (furthermore, I try to adhere to skinny-controller philosophy)

If you don't like changing the Model's subquery method to return a type of "mixed" (array or false) then I think checking for an empty result before trigging the subquery is a reasonable compromise.... I can't imagine why you'd want to run a subquery on an empty condition set. If you agree, I'll refactor to check for empy instead of false.

@real34

+1 for this PR. It is sometimes useful to avoid doing a subquery, and for now it is kind of hacky.

An use case for this is a subquery depending on two fields (date min AND date max). It will be defined on one of both in search filters, and return nothing if the second value is empty.

@skie skie reopened this
@ajibarra

@zeroasterisk was asked to make a new pull request according to latest changes on master.

@ajibarra ajibarra closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 5, 2012
  1. @zeroasterisk

    A minor update to searchable, which allows you to pass FALSE from you…

    zeroasterisk authored
    …r 'subquery' [_addCondSubquery()] and 'query' [_addCondQuery()] methods and bypass the addition to the search criteria -- this allows you to break out of passing a search condition based on some 'special case' values or processing... cheap and easy
Commits on Mar 6, 2012
  1. @zeroasterisk
This page is out of date. Refresh to see the latest.
Showing with 6 additions and 2 deletions.
  1. +6 −2 models/behaviors/searchable.php
View
8 models/behaviors/searchable.php
@@ -251,7 +251,9 @@ protected function _addCondValue(Model $model, &$conditions, $data, $field) {
protected function _addCondQuery(Model $model, &$conditions, $data, $field) {
if ((method_exists($model, $field['method']) || $this->__checkBehaviorMethods($model, $field['method'])) && (!empty($data[$field['name']]) || (isset($data[$field['name']]) && ($data[$field['name']] === 0 || $data[$field['name']] === '0')))) {
$conditionsAdd = $model->{$field['method']}($data);
- $conditions = array_merge($conditions, (array)$conditionsAdd);
+ if ($conditionsAdd !== false) {
+ $conditions = array_merge($conditions, (array)$conditionsAdd);
+ }
}
return $conditions;
}
@@ -291,7 +293,9 @@ protected function _addCondSubquery(Model $model, &$conditions, $data, $field) {
$fieldName = $field['field'];
if ((method_exists($model, $field['method']) || $this->__checkBehaviorMethods($model, $field['method'])) && (!empty($data[$field['name']]) || (isset($data[$field['name']]) && ($data[$field['name']] === 0 || $data[$field['name']] === '0')))) {
$subquery = $model->{$field['method']}($data);
- $conditions[] = array("$fieldName in ($subquery)");
+ if ($subquery !== false) {
+ $conditions[] = array("$fieldName in ($subquery)");
+ }
}
return $conditions;
}
Something went wrong with that request. Please try again.