yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.9k forks source link

Suggested behavior for Query #2002

Closed tonydspaniard closed 10 years ago

tonydspaniard commented 10 years ago

Currently in order to make a search function (not following gii approach) a user needs to do the following:

$query = Contact::find();
if(!empty($this->full_name))
{
   $query->where(['like', 'full_name', $this->full_name]);
}
// or 
if(trim($this->email) !== '')
{
   $query->where(['email' => $this->email]);
}

Or using the gii approach (creating an addCondition method) it is always required to search for empty or NULL values. Which even I think is good for demo purposes to have the addCondition on the search model reproduced by gii, I find it quite of non-sense to have the same method on each search model.

Failing to not check against NULL values will cause two different results on both scenarios. On the first one like, it throws an error:

operator_like_requires_two_operands

Whereas on the second one will produce the following SQL:

SELECT * FROM tbl_contact WHERE full_name IS NULL LIMIT 10

Which IMHO I think is the wrong behavior, even if the adCondition function of the search model strictly checks for empty strings not NULL attributes.

_Suggestion_

I believe that everybody should be able to use the following:

$query = Contact::find();
$query->where(['like', 'full_name', $this->full_name]);
$query->where(['email' => $this->email]);

And NULL or empty values should be removed from the resulting Query object as it used to happen with CDbCriteria on Yii1. That will make our search classes cleaner and without the need to check whether I have NULL or empty values on my queries and also we will follow an approach that was commonly used by new users.

I do not think we should force the IS NULL search if an attribute has null value. For that purposes we already have yii\db\Expression don't you agree? I think it makes more sense and is less error prone (in terms of search results).

$query->where(['email' => yii\db\Expression('IS NULL')]);
Sammaye commented 10 years ago

That interesting for ES cos filter can be used in more ways than that depending on exactly how you wish to filter, I mean personally in my ES querying it is situated within a filtered property that has a query and filter property within it all topped off with being put in a normal query object but it c an be situated in different places depending upon the type of query you wanna do.

From basic looking I am unsure if filter() in the ES Query actually works in all situations.

samdark commented 10 years ago

Renamed ES method to applyFilter

samdark commented 10 years ago

Using filter in method names isn't good since it conflicts with existing Elasticsearch term.

Alternatives:

qiangxue commented 10 years ago

None of the alternatives is good. We should rename ES because Query::filter() is much more commonly used than ES filter.

qiangxue commented 10 years ago

We may consider filterPart and queryPart for ES.