where is essential when querying in Yii Model.
Where method is declared as
where the parameter $condition type is string or array
1. String
String is the simplest, just press in sql Just write the where condition, such as
2. Array
If it is an array, there are two formats Writing method.
First way of writing:
If value is a string or number, etc., then the generated conditional statement format is column1=value1 AND column2=value2 AND ....
['type' => ; 1, 'status' => 2]
//Generate
(type = 1) AND (status = 2)
If the value is an array, then the IN statement in sql will be generated ;
['id' => [1, 2, 3], 'status' => 2]
//Generate
(id IN (1, 2, 3)) AND (status = 2)
If value is Null, then the Is Null statement will be generated.
['status' => null]
//Generate
status IS NULL
The second way of writing will generate different sql conditions based on different operators.
['and', 'id=1', 'id=2']
// Generate
id=1 AND id=2
If an operand is also an array, it will be converted to a string in the following format, such as['and', 'type=1', ['or', 'id=1', 'id=2']]
//Generate
type=1 AND (id=1 OR id= 2)
Note: This method does not reference or encode.['between', 'id', 1, 10]
//Generate
id BETWEEN 1 AND 10
['in', 'id', [1, 2, 3]]
//Generate
id IN (1, 2, 3)
Note: This method will reference the column and also encode the values in the array.['like', 'name', 'tester']
//Generate
name LIKE '%tester%'
If the value is an array, multiple like statements will be generated and used AND to connect. like['like', 'name', ['test', 'sample']]
//Generate
name LIKE '%test%' AND name LIKE '%sample%'
Note: This Methods reference columns and encode values in arrays.['like', 'name', '%tester', false]
//Generate
name LIKE '%tester'
The above introduces the Yii20 Chinese development guide - full analysis of Where condition query, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.