Yii20 Chinese Development Guide - Full Analysis of Where Conditional Query

WBOY
Release: 2016-08-08 09:18:57
Original
1279 people have browsed it

where is essential when querying in Yii Model.
Where method is declared as

  1. static where( $condition )


where the parameter $condition type is string or array
1. String
String is the simplest, just press in sql Just write the where condition, such as

  1. $condition = 'name='zhidemy.com' and age>10';

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

  • 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 the 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.
    • 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: and between are similar.
    • 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 used AND to connect. like

      ['like', 'name', ['test', 'sample']]

      //Generate

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

      Note: This Methods reference columns and encode values ​​in arrays.
      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, only in When the second parameter is an array, use or to connect multiple like statements.
    • not like: is similar to like.
    • or not like: is similar to or like.

The above introduces the Yii20 Chinese development guide - full analysis of Where condition query, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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