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. 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:

$ 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:

$ 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:

$ User-> where ('Id = 1')-> field ('Id, name, email ')-> find ();

$ 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 is used to query or update the defined strings, arrays, and objects of conditions.

Table is used to define the name string and array of the data table to be operated.

Alias is used to define an alias string for the current data table.

Data is used to assign values to arrays and objects of data objects before data is added or updated.

Field is used to define the fields to be queried (supports field exclusion) strings and arrays

Order is used to sort the result strings and arrays.

Limit is used to limit the number of query results. strings and numbers

Page is used to query the strings and numbers of pages (which are converted to limit internally ).

Group is used to support strings for the queried group.

Having is used to support query having strings.

Join * supports strings and arrays for join queries.

Union * is used to support strings, arrays, and objects for the union query.

The distinct used for query supports boolean values.

Boolean value of the lock mechanism used by the database

The cache is used to query the cache. multiple parameters are supported (detailed descriptions will be made later in the cache section)

Relation is used for association query (support for extension of association model is required) string

Validate is used to automatically verify the array of data

Auto is used to automatically complete the array of data

Filter is used to filter strings.

Scope * is used to name the 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)

Parameter where (required): query or operation condition, supporting strings, arrays, and objects

Returns the current model instance.

Note: If the where method is not called, 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)

Parameter table (required): name of a data table. multiple tables can be operated, and strings, arrays, and objects are supported.

Returns the current model instance.

Note: If the table method is not called, the data table corresponding to or defined by the model is automatically obtained.

Usage example:

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

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

$ 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:

$ 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)

Parameter data (required): data, supporting arrays and objects

Returns the current model instance.

Note: 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:

$ Model-> data ($ data)-> add ();

$ 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:

$ This-> find (3 );

$ 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.

Returns the current model instance.

Note: If the field method is not called, all fields are returned by default, which is equivalent to field ('*').

Example:

$ Model-> field ('Id, nickname as name')-> select ();

$ 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:

$ 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:

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

Obtains all fields except status.

ORDER

Order is used to sort operation results

Usage order ($ order)

The order parameter (required) is the name of the sorted field. it can be a string or an array or multiple fields.

Returns the current model instance.

Note: If you do not call the order method, follow the default database rules.

Example:

Order ('Id desc ')

The sorting method supports sorting multiple fields.

Order ('status desc, id asc ')

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

Order (array ('status' => 'desc', 'id '))

LIMIT

Limit is used to define the results limit to be queried (all database types are supported)

Use limit ($ limit)

Parameter limit (required): limit the number of strings supported

Returns the current model instance.

Note: 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:

Limit ('1, 10 ')

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

Limit (1, 10)

If you use

Limit ('10 ')

Equivalent

Limit ('0, 10 ')

PAGE

Page is used to define the data page to be queried.

Usage page ($ page)

Parameter page (required): paging, string supported

Returns the 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:

Page ('Page [, listRows] ')

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

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:

Page (2, 10 );

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

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:

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

And previous usage

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

Equivalent.

GROUP

Group is supported for group queries of databases.

Usage group ($ group)

The group parameter (required) is the field name of the group. strings are supported.

Returns the current model instance.

Remarks none

Example:

Group ('User _ id ')

The Group method supports only strings.

HAVING

Having is used for database having query support

Usage having ($ having)

Having (required): having, a string supported

Returns the current model instance.

Remarks none

Example:

Having ('User _ id> 0 ')

The having method supports only strings.

JOIN

Join is supported by join queries for databases.

Usage join ($ join)

Join parameter (required): join operation, supporting strings and arrays

Returns the current model instance.

Note: The join method supports multiple calls.

Example:

$ 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

$ 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:

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.

Use union ($ union, $ all = false)

The union parameter (required) is a union operation that supports strings, arrays, and objects.
All (optional): whether to use the union all operation. the default value is false.

Returns the current model instance.

Note: The Union method supports multiple calls.

Example:

$ Model-> field ('name ')

-> Table ('think _ user_0 ')

-> Union ('select name FROM think_user_1 ')

-> Union ('select name FROM think_user_2 ')

-> Select ();

Array usage:

$ Model-> field ('name ')

-> Table ('think _ user_0 ')

-> Union (array ('field' => 'name', 'table' => 'think _ user_1 '))

-> Union (array ('field' => 'name', 'table' => 'think _ user_2 '))

-> Select ();

Or

$ Model-> field ('name ')

-> Table ('think _ user_0 ')

-> Union (array ('select name FROM think_user_1 ', 'Select name FROM think_user_2 '))

-> Select ();

Supports union all operations, for example:

$ Model-> field ('name ')

-> Table ('think _ user_0 ')

-> Union ('select name FROM think_user_1 ', true)

-> Union ('select name FROM think_user_2 ', true)

-> Select ();

Or

$ Model-> field ('name ')

-> Table ('think _ user_0 ')

-> Union (array ('select name FROM think_user_1 ', 'Select name FROM think_user_2'), true)

-> 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)

Distinct (required): whether to use distinct. Boolean value is supported.

Returns the current model instance.

Remarks none

Example:

$ Model-> Distinct (true)-> field ('name')-> select ();

LOCK

Lock is used for query or write lock

Usage lock ($ lock)

Lock (required): whether to lock; Boolean value supported

Returns the current model instance.

Note: 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:

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

Use validate ($ validate)

Parameter validate (required): automatically verifies the definition

Returns the current model instance.

Remarks 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)

Auto (required): automatic completion of definition

Returns the current model instance.

Note: 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)

Scope (required): name range definition

Returns the current model instance.

Note that the scope method is actually a preference for 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)

Filter (required): filter method name

Returns the current model instance.

Note: The filter method is generally used for write and update operations.

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

$ 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.

The above is ThinkPHP3.1 quick start (4) consistent operation content. For more information, see PHP Chinese network (www.php1.cn )!

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.