Where is necessary when querying in Yii's model.
The Where method is declared as
- static where ($condition)
Where the parameter $condition type is a string or an array
1. String
The string is the simplest and can be written directly to the Where condition in SQL, as
- $condition = ' name=\ ' zhidemy.com\ ' and age>10 ';
2. Arrays
In the case of arrays, there are two ways to format the notation.
- Name-value format Dictionary array: [' column1 ' = + value1, ' column2 ' = value2, ...]
- Logical operator Format: [operator, Operand1, Operand2, ...]
The first type of notation:
If the value is a string or a number, the resulting conditional statement is formatted as column1=value1 and Column2=value2 and ....
[' type ' = 1, ' status ' + 2]
Generated
(type = 1) and (status = 2)
If the value is an array, the in statement in SQL is generated;
[' id ' = [1, 2, 3], ' status ' = 2]
Generated
(ID in (1, 2, 3)) and (status = 2)
If the value is null, an IS null statement is generated.
[' status ' = null]
Generated
Status is NULL
The second notation generates different SQL conditions based on the different operators.
- and : all the operands are connected using and. Such as
[' and ', ' id=1 ', ' id=2 ']
Generated
Id=1 and id=2
If an operand is an array, it is converted to a string in the following format, such as
[' and ', ' type=1 ', [' or ', ' id=1 ', ' id=2 ']
Generated
Type=1 and (id=1 OR id=2)
Note: This method does not refer to or encode the operation.
- or: similar to and, except that the operand is connected with or .
- between: The first operand is the name of the column, the second and third operands are the minimum and maximum values for the range. Such as
[' Between ', ' id ', 1, 10]
Generated
ID between 1 and 10
- Not between: similar to between .
- In : The first operand is a column or DB expression, and the second operand is an array, as
[' in ', ' id ', [1, 2, 3]
Generated
ID in (1, 2, 3)
Note: This method references the column, and the values in the array are encoded.
- Not in : similar to the in above.
- Like : The first operand is a column or DB expression, the second operand is a string or an array such as
[' Like ', ' name ', ' tester ']
Generated
Name like '%tester% '
If the value is an array, multiple like statements are generated and connected with and. Such as
[' Like ', ' name ', [' Test ', ' sample ']
Generated
Name like '%test% ' and name '%sample% '
Note: This method references the column, and the values in the array are encoded.
Sometimes you may need to handle the% yourself, then you can use the third parameter:
[' Like ', ' name ', '%tester ', false]
Generated
Name like '%tester '
- or like: the same as similar, except that the second parameter is an array with or to concatenate multiple as statements.
- Not as : similar to like.
- or not: similar to or like.
The above describes the Yii20 Chinese Development Wizard--where condition query full analysis, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.