Additional conditions
You can use Andwhere () or orwhere () to attach additional conditions based on the original condition. You can call these methods multiple times to append different conditions separately. For example
$status = ten;
$search = ' Yii ';
$query->where ([' status ' = $status]);
if (!empty ($search)) {
$query->andwhere ([' Like ', ' title ', $search]);
}
If the $search is not empty, the following SQL statement will be generated:
... WHERE (' status ' = Ten) and (' title ' Like '%yii% ')
Filter conditions
When the WHERE condition comes from the user's input, you usually need to ignore the null value entered by the user. For example, in a form that can be searched by user name or mailbox, the user name or the mailbox input box does not enter anything, in which case you want to ignore the corresponding search criteria, then you can use the Yii\db\query::filterwhere () method to achieve this purpose:
$username and $email input from the user
$query->filterwhere ([
' username ' = $username,
' email ' + $email, c8/>]);
The only difference between yii\db\query::filterwhere () and where () is that the former ignores the null value of the hash format in the condition. So if email is empty and email is empty and username is not empty, then the above code will eventually produce the following SQL ... WHERE Username=:username.
tip: When a value is null, an empty array, an empty string, or one that contains only whitespace characters, it is judged to be a null value.
Similar to [Yii\db\query::andwhere () |andwhere ()]] and Orwhere (), you can use Yii\db\query::andfilterwhere () and Yii\db\query::o Rfilterwhere () method to append additional filtering conditions.
Both andwhere and andfilterwhere can be used to append conditions, except that Andfilterwhere ignores null values in the condition