Yii2.0 Chinese development wizard -- full parsing of Where condition query, _ PHP Tutorial

Source: Internet
Author: User
Yii2.0 Chinese development wizard-full resolution of Where condition query ,. Yii2.0 Chinese development wizard-Where condition query full resolution, where is essential when querying in the Yii Model. The Where method is declared as the parameter $ condition type is the word Yii2.0 Chinese development wizard -- Where condition query full resolution,

The where clause is required when querying in the Yii Model.
The Where method is declared


The $ condition type is a string or array.

1. string
The string is the simplest and can be directly written according to the where condition in SQL, as shown in

2. array
If it is an array, there are two formats.

  • Name-value dictionary array: ['column1 '=> value1, 'column2' => value2,...]
  • Logical operator format: [operator, operand1, operand2,...]

Method 1:
If the value is a string or number, the format of the generated condition statement is column1 = value1 AND column2 = value2 AND ....

['Type' => 1, 'status' => 2]

// Generate

(Type = 1) AND (status = 2)


If the value is an array, an IN statement IN SQL is generated;

['Id' => [1, 2, 3], 'status' => 2]

// Generate

(Id IN (1, 2, 3) AND (status = 2)

If the value Is Null, A Is Null statement Is generated.

['Status' => null]

// Generate

Status IS NULL

The second write method generates different SQL conditions based on different operators.

    • And: uses AND to connect all operands. For example

      ['And', 'id = 1', 'id = 2']

      // Generate

      Id = 1 AND id = 2

      If an operation number is an array, it is converted to a string in the following format, as shown in figure

      ['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 data.
    • Or: similar to and, it only uses OR to connect the operands.
    • Between: The first operand is the column name, and the second and third operands are the minimum and maximum values of the range. For example

      ['Between', 'id', 1, 10]

      // Generate

      Id BETWEEN 1 AND 10

    • Not between: Similar to.
    • In: the first operand is a column or DB expression, and the second operand is an array, as shown in

      ['In', 'id', [1, 2, 3]

      // Generate

      Id IN (1, 2, 3)

      Note: This method will reference the column and encode the values in the array.
    • Not in: similar to the preceding in.
    • Like: the first operand is a column or DB expression, and the second operand is a string or array, such

      ['Like', 'name', 'tester']

      // Generate

      Name LIKE '% tester %'

      If the value is an array, multiple like statements are generated AND connected with AND. For example

      ['Like', 'name', ['test', 'sample']

      // Generate

      Name LIKE '% test %' AND name LIKE '% sample %'

      Note: This method will reference the column and encode the values in the array.
      Sometimes you may need to process % by yourself, you can use the third parameter:

      ['Like', 'name', '% tester', false]

      // Generate

      Name LIKE '% Tester'

    • Or like: Similar to like, but uses or to connect multiple like statements when the second parameter is an array.
    • Not like: Similar to like.
    • Or not like: similar to or like.

When querying in the Yii Model, where is essential. Where method declaration is Where parameter $ condition type is word...

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.