Home > Backend Development > PHP Tutorial > Yii2.0 Chinese Development Guide - Full analysis of Where condition query, _PHP tutorial

Yii2.0 Chinese Development Guide - Full analysis of Where condition query, _PHP tutorial

WBOY
Release: 2016-07-13 09:44:57
Original
1120 people have browsed it

Yii2.0 Chinese Development Guide - Full analysis of Where condition query,

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.

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

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: will use AND to connect all operands. like

      ['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.
    • or: is similar to and, except that OR is used to connect the operands.
    • between: The first operand is the name of the column, the second and third operands are the minimum and maximum values ​​of the range. like

      ['between', 'id', 1, 10]

      //Generate

      id BETWEEN 1 AND 10

    • not between: is similar to between.
    • in: The first operand is a column or DB expression, and the second operand is an array, such as

      ['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.
    • not in: is similar to in above.
    • like: The first operand is a column or DB expression, and the second operand is a string or array, such as

      ['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.
      Sometimes you may need to handle % yourself, then you can use the third parameter:

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

      //Generate

      name LIKE '%tester'

    • or like: is similar to like, except that or is used to connect multiple like statements when the second parameter is an array.
    • not like: is similar to like.
    • or not like: is similar to or like.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1043455.htmlTechArticleYii2.0 Chinese Development Guide - Full analysis of Where condition query, when querying where in Yii Model is essential. Where method is declared as where parameter $condition type is word...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template