Where is essential when querying in Yii Model.
Where method is declared as
The parameter $condition type is string or array
1. String
String is the simplest. Just write it directly according to the where condition in sql, such as
2. Array
If it is an array, there are two ways to write it.
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 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 the value.['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 connected using AND. like['like', 'name', ['test', 'sample']]
//Generate
name LIKE '%test%' AND name LIKE '%sample%'
Note: This method will reference the column and also encode the values in the array.['like', 'name', '%tester', false]
//Generate
name LIKE '%tester'