Thinkphp model filters query fields and Expression Methods

Source: Internet
Author: User

Use a query expression
Query expression format:
$ Map ['field name'] = array ('expression', 'query condition ');
The expressions are case-insensitive and support the following Query expressions:
EQ: equal to (=)
Example: $ map ['id'] = array ('eq ', 100 );
It is equivalent to the following query:
$ Map ['id'] = 100;
The query condition is id = 100.
NEQ: not equal (! =)
Example: $ map ['id'] = array ('neq', 100 );
The query condition is id! = 100
GT: greater than (>)
Example: $ map ['id'] = array ('gt ', 100 );
Indicates that the query condition is id> 100.
EGT: greater than or equal to (> =)
Example: $ map ['id'] = array ('egt', 100 );
The query condition is id> = 100.
LT: less than (<)
Example: $ map ['id'] = array ('lt ', 100 );
The query condition is id <100
ELT: less than or equal to (<=)
Example: $ map ['id'] = array ('elt', 100 );
The query condition is id <= 100
LIKE: similar to SQL LIKE
Example: $ map ['name'] = array ('like', 'thinkphp % ');
The query condition becomes name like 'thinkphp %'
If the DB_LIKE_FIELDS parameter is configured, some fields are automatically fuzzy queried. For example:
'Db _ LIKE_FIELDS '=> 'title | Content'
Use
$ Map ['title'] = 'thinkphp ';
The query condition will change to name like '% thinkphp %'
[NOT] BETWEEN: Same as SQL [not] between. query conditions support strings or arrays, for example:
$ Map ['id'] = array ('between', '1, 8 ');
It is equivalent to the following:
$ Map ['id'] = array ('between', array ('1', '8 '));
The query condition becomes id BETWEEN 1 AND 8.
[NOT] IN: Same as SQL [not] in. query conditions support strings or arrays, for example:
$ Map ['id'] = array ('not in', '1, 5, 8 ');
It is equivalent to the following:
$ Map ['id'] = array ('not in', array ('1', '5', '8 '));
The query condition becomes id not in (, 8)
EXP: expression that supports more complex queries
For example:
$ Map ['id'] = array ('in', '1, 3, 8 ');
You can change it:
$ Map ['id'] = array ('exp ', 'in (1, 3, 8 )');
Exp query conditions are not treated as strings, so the following query conditions can use any syntax supported by SQL, including using functions and field names. A query expression can be used not only for query conditions, but also for data updates. For example:
$ User = M ("User"); // instantiate the User object
// Assign values to the attributes of the data object to be modified
$ Data ['name'] = 'thinkphp ';
$ Data ['score '] = array ('exp', 'score + 1'); // Add 1 to your credit
$ User-> where ('Id = 5')-> save ($ data); // save the modified data according to the conditions.

The above filter query conditions can be written in the _ filter () function, for example:
// Filter query Fields
Function _ filter (& $ map ){
$ Map ['title'] = array ('like', "%". $ _ POST ['til ']. "% ");
$ Map ['categoryid'] = array ('eq ', $ _ REQUEST ['cid']);

}

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.