ThinkPHP3.1 quick start (4) consistent operations

Source: Internet
Author: User
In the previous article, we described the usage of the query language in detail, but the query language only solves the query or operation conditions. More cooperation also requires the use of the coherent operation methods provided by the model. Introducing coherent operations can effectively improve data access code definition and development efficiency, and support all... "> <LINKhref =" http://www.php100.com//statics/style/headflo

In the previous article, we described the usage of the query language in detail, but the query language only solves the query or operation conditions. More cooperation also requires the use of the coherent operation methods provided by the model.

Introduction

Consistent operations can effectively improve the code definition and development efficiency of data access, and support all CURD operations, which is also a highlight of ThinkPHP's ORM. It is also relatively simple to use. If we want to query the first 10 records that meet the requirement of a User table and want to sort the records according to the User creation time, the code is as follows:

  1. $ User-> where ('status = 1')-> order ('create _ time')-> limit (10)-> select ();

The where, order, and limit methods here are called coherent operation methods. Except that the select method must be placed in the last one (because the select method is not a coherent operation method ), the call sequence of the methods for a coherent operation is not sequential. For example, the following code is equivalent to the preceding one:

  1. $ User-> order ('create _ time')-> limit (10)-> where ('status = 1')-> select ();

In fact, not only query methods can use consistent operations, but also all CURD methods can be used. For example:

  1. $ User-> where ('Id = 1')-> field ('Id, name, email ')-> find ();
  2. $ User-> where ('status = 1 and id = 1')-> delete ();

A coherent operation is only valid for a query or operation. After the operation is completed, all values of a coherent operation are automatically cleared. (some special coherent operations record the current value, such as cache coherent operations ). In short, the result of a coherent operation is not included in future queries.
The following methods are supported:

Method Function Supported parameter types
Where Definitions used to query or update conditions String, array, and object
Table Used to define the name of the data table to be operated String and array
Alias Used to define aliases for the current data table String
Data Assign values to data objects before adding or updating data Array and object
Field Defines the fields to be queried (supports field exclusion) String and array
Order Used to sort results String and array
Limit Used to limit the number of query results String and number
Page Used for querying pages (converted to limit internally) String and number
Group Used to support the query group String
Having Used to Support query having String
Join * Used to Support join queries String and array
Union * Used to support union queries String, array, and object
Distinct Distinct support for queries Boolean value
Lock Database lock mechanism Boolean value
Cache Used to query cache Multiple parameters are supported (detailed descriptions will be made later in the cache)
Relation Used for association query (support for extension of association model is required) String
Validate Used for automatic data verification Array
Auto Used for automatic data completion Array
Filter Used for data filtering String
Scope * Used for naming range String, array

All the coherent operations return the current model instance object (this), with the * identifier supporting multiple calls.

Usage

Since the use of coherent operations often involves the joint use of multiple methods, the following describes the basic usage of each coherent operation:

WHERE
Where is used to query or update conditions.
Usage Where ($ where)
Parameters Where (required): query or operation condition, supporting strings, arrays, and objects
Return value Current model instance
Remarks If the where method is not called, the update and delete operations are not performed by default.

The Where method is the most commonly used coherent operation method. For more detailed usage, see quick start (3) query language.


TABLE
Table defines the name of the data table to be operated. Dynamically change the name of the data table for the current operation. You need to write the full name of the data table, including the prefix. Aliases and cross-database operations can be used.
Usage Table ($ table)
Parameters Table (required): name of a data table. It supports operations on multiple tables and strings, arrays, and objects.
Return value Current model instance
Remarks If the table method is not called, the data table corresponding to or defined by the model is automatically obtained.

Usage example:

  1. $ Model-> Table ('think _ user user')-> where ('status> 1')-> select ();

You can also perform cross-database operations in the table method, for example:

  1. $ Model-> Table ('Db _ name. think_user user')-> where ('status> 1')-> select ();

The parameters of the Table method support strings and arrays. The usage of the array method is as follows:

  1. $ Model-> Table (array ('think _ user' => 'user', 'think _ group' => 'Group ')) -> where ('status> 1')-> select ();

The advantage of using arrays to define is to avoid errors due to table name and keyword conflicts.
Generally, you do not need to call the table method. By default, the data table corresponding to or defined in the current model is automatically obtained.

DATA
Data can be used to assign values to data objects before adding or saving data.
Usage Data ($ data)
Parameters Data (required): data, supporting arrays and objects
Return value Current model instance
Remarks If the data method is not called, the current data object will be taken or the data of add and save will be passed in.

Example:

  1. $ Model-> data ($ data)-> add ();
  2. $ Model-> data ($ data)-> where ('Id = 3')-> save ();

Parameters of the Data method support objects and arrays. If an object is an object, it is automatically converted to an array. If you do not define a value assignment for the data method, you can also use the create method or manually assign a value to the data object.

In addition to creating data objects, the data method of the model can also read the current data object,
For example:

  1. $ This-> find (3 );
  2. $ Data = $ this-> data ();
FIELD
Field is used to define the field to be queried.
Usage Field ($ field, $ struct T = false)
Parameters

Field (required): field name. It can be a string or an array or a field alias. If it is true, it indicates the explicit or all fields in the data table.

Exclude T (optional): whether to exclude. The default value is false. If it is true, all fields defined are excluded from the field parameter definition in the data table.

Return value Current model instance
Remarks If the field method is not called, all fields are returned by default, which is equivalent to field ('*').

Example:

  1. $ Model-> field ('Id, nickname as name')-> select ();
  2. $ Model-> field (array ('id', 'nickname' => 'name')-> select ();

If the field method is not called or the input parameter of the field method is null, it is equivalent to using field.
To explicitly input all fields, use the following method:

  1. $ Model-> field (true)-> select ();

However, we recommend that you only obtain the field name that needs to be explicitly specified or use the field exclusion method to define it. For example:

  1. $ Model-> field ('status', true)-> select ();

Obtains all fields except status.

ORDER
Order is used to sort operation results
Usage Order ($ order)
Parameters Order (required): name of the sorted field. It can be a string or an array or multiple fields.
Return value Current model instance
Remarks If you do not call the order method, follow the default database rules.

Example:

  1. Order ('Id desc ')

The sorting method supports sorting multiple fields.

  1. Order ('status desc, id asc ')

The parameters of the order method support strings and arrays. The array usage is as follows:

  1. Order (array ('status' => 'desc', 'id '))
LIMIT
Limit is used to define the results limit to be queried (all database types are supported)
Usage Limit ($ limit)
Parameters Limit (required): limit the number of strings supported
Return value Current model instance
Remarks If the limit method is not called, there is no limit.

Note: If the limit method is not called, there is no limit.
We know that the limit usage of different database types is different, but ThinkPHP usage is always a unified method, that is, limit ('offset, length '), both Mysql, SQL Server, and Oracle databases are used in this way. The system's database driver class is responsible for solving this difference.
Example:

  1. Limit ('1, 10 ')

You can also use the following statement, which is equivalent:

  1. Limit (1, 10)

If you use

  1. Limit ('10 ')

Equivalent

  1. Limit ('0, 10 ')
PAGE
Page is used to define the data page to be queried.
Usage Page ($ page)
Parameters Page (required): paging, string supported
Return value Current model instance
Remarks None

The Page operation method is a new feature that allows you to perform paging queries more quickly.
The usage of the Page method is similar to that of the limit method. The format is:

  1. Page ('Page [, listRows] ')

Page indicates the current Page number, and listRows indicates the number of records displayed on each Page. For example:

  1. Page ('2, 10 ')

It indicates that the data on the 2nd page is obtained when 10 records are displayed on each page.
The following statement is equivalent:

  1. Page (2, 10 );

If listRow is not written, the limit ('length') value will be read, for example:

  1. Limit (25)-> page (3 );

If 25 records are displayed on each page, the data on the 3rd page is obtained.
If limit is not set, 20 records are displayed on each page by default.
The page method supports the second parameter, for example:

  1. $ This-> page (5, 25)-> select ();

And previous usage

  1. $ This-> limit ('5, 25')-> select ();

Equivalent.

GROUP
Group is supported for group queries of databases.
Usage Group ($ group)
Parameters Group (required): the field name of the group. Strings are supported.
Return value Current model instance
Remarks None

Example:

  1. Group ('User _ id ')

The Group method supports only strings.

HAVING
Having is used for database having query support
Usage Having ($ having)
Parameters Having (required): having, supporting strings
Return value Current model instance
Remarks None

Example:

  1. Having ('User _ id> 0 ')

The having method supports only strings.

JOIN
Join is supported by join queries for databases.
Usage Join ($ join)
Parameters Join (required): join operation, supporting strings and arrays
Return value Current model instance
Remarks The join method supports multiple calls.

Example:

  1. $ Model-> join ('Work ON artist. id = work. artist_id ')-> join ('card ON artist. card_id = card. Id')-> select ();

The left join method is used by default. If you want to use another JOIN method, you can change it

  1. $ Model-> join ('right JOIN work ON artist. id = work. artist_id ')-> select ();

If the parameters of the join method use arrays, the join method can only be used once and cannot be used together with the string method.
For example:

  1. Join (array ('Work ON artist. id = work. artist_id ', 'card ON artist. card_id = card. Id '))
UNION
Union is used for union queries in databases.
Usage Union ($ union, $ all = false)
Parameters Union (required): union operation, supporting strings, arrays, and objects
All (optional): whether to use the union all operation. The default value is false.
Return value Current model instance
Remarks The Union method supports multiple calls.

Example:

  1. $ Model-> field ('name ')
  2. -> Table ('think _ user_0 ')
  3. -> Union ('select name FROM think_user_1 ')
  4. -> Union ('select name FROM think_user_2 ')
  5. -> Select ();

Array usage:

  1. $ Model-> field ('name ')
  2. -> Table ('think _ user_0 ')
  3. -> Union (array ('field' => 'name', 'table' => 'think _ user_1 '))
  4. -> Union (array ('field' => 'name', 'table' => 'think _ user_2 '))
  5. -> Select ();

Or

  1. $ Model-> field ('name ')
  2. -> Table ('think _ user_0 ')
  3. -> Union (array ('select name FROM think_user_1 ', 'Select name FROM think_user_2 '))
  4. -> Select ();

Supports union all operations, for example:

  1. $ Model-> field ('name ')
  2. -> Table ('think _ user_0 ')
  3. -> Union ('select name FROM think_user_1 ', true)
  4. -> Union ('select name FROM think_user_2 ', true)
  5. -> Select ();

Or

  1. $ Model-> field ('name ')
  2. -> Table ('think _ user_0 ')
  3. -> Union (array ('select name FROM think_user_1 ', 'Select name FROM think_user_2'), true)
  4. -> Select ();

Each union method is equivalent to an independent SELECT statement.
Note: SELECT statements within UNION must have the same number of columns. Columns must also have similar data types. In addition, the columns in each SELECT statement must be in the same order.

DISTINCT
Unique filtering during data query by distinct
Usage Distinct ($ distinct)
Parameters Distinct (required): whether to use distinct. Boolean value is supported.
Return value Current model instance
Remarks None

Example:

  1. $ Model-> Distinct (true)-> field ('name')-> select ();
LOCK
Lock is used for query or write lock
Usage Lock ($ lock)
Parameters Lock (required): whether to lock; Boolean value supported
Return value Current model instance
Remarks The join method supports multiple calls.

The Lock method is used for the database Lock mechanism. If you use the Lock method when querying or performing operations:

  1. Lock (true)

The for update or for update nowait (Oracle database) is automatically added at the end of the generated SQL statement ).

VALIDATE
Validate is used for automatic data verification
Usage Validate ($ validate)
Parameters Validate (required): automatically verifies the definition
Return value Current model instance
Remarks It can only be used with the create method

The validate method is used for automatic data verification. We will describe it in detail in the data verification section.

AUTO
Auto is used to automatically complete data
Usage Auto ($ auto)
Parameters Auto (required): automatic completion of definition
Return value Current model instance
Remarks The auto method can only be used with the create method.

The auto method is used to automatically complete data operations. We will describe how to use the auto method in the auto complete part of data.

SCOPE
Scope is used to name a model.
Usage Scope ($ scope)
Parameters Scope (required): name range definition
Return value Current model instance
Remarks The scope method is actually a predefined definition of coherent operations.

For details about how to use the scope method, refer to the new feature naming range in 3.1.

FILTER
Filters are used for data security filtering.
Usage Filter ($ filter)
Parameters Filter (required): filter method name
Return value Current model instance
Remarks The filter method is generally used for write and update operations.

The filter method is used to filter data objects securely. For example:

  1. $ Model-> data ($ data)-> filter ('strip _ tags')-> add ();

Currently, the filter method does not support filtering multiple methods.

Summary

Consistent operations bring great convenience to our data operations. As long as SQL can be implemented, ThinkPHP can basically be used for consistent operations, it does not need to consider the differences in expressions between databases and is portable. We will explain how to operate and obtain variables later.

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.