The difference between Andfilterwhere and Andwhere in the YII framework

Source: Internet
Author: User
Tags mongodb postgresql redis yii


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


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.