Home> PHP Framework> ThinkPHP> body text

ThinkPHP: One of the three most powerful tools for models (searcher)

爱喝马黛茶的安东尼
Release: 2019-12-16 16:51:50
forward
3126 people have browsed it

ThinkPHP: One of the three most powerful tools for models (searcher)

[√New skill] Searcher - unified management of your search code

The model searcher is an automatic The third unified management tool after the model getter and modifier functions is mainly used to encapsulate query condition expressions of fields (or search identifiers). A searcher corresponds to a special method (the method must be of public type). The method naming specification is: searchFieldNameAttr (FieldName is the camel case conversion of the data table field). The searcher is only triggered when the withSearch method is called.

Usage scenarios of the searcher include:

·Restrict and standardize the search conditions of the form;

·Predefined query conditions simplify querying.

For example, if we need to define a searcher for the name field and time field for the User model, we can use:

where('name','like', $value . '%'); } public function searchCreateTimeAttr($query, $value, $data) { $query->whereBetweenTime('create_time', $value[0], $value[1]); } }
Copy after login

The searcher method has three parameters, the first is the query object, and the second The first is the value of the current search identifier, and the third is all current search data (optional).

Then, we can use the following query

User::withSearch(['name', 'create_time'], [ 'name'=>'think', 'create_time'=>['2018-8-1','2018-8-5'], 'status'=>1 ]) ->select();
Copy after login

The final generated SQL statement is similar to

SELECT * FROM `think_user` WHERE `name` LIKE 'think%' AND `create_time` BETWEEN '2018-08-01 00:00:00' AND '2018-08-05 00:00:00'
Copy after login

You can see that there is no data in the status field in the query conditions, so you can It is very good to avoid illegal query conditions of the form being passed in. In this example, only the name and create_time conditions can be used for query.

In fact, in addition to using query expressions in the searcher, you can also use any other query constructor and chain operations.

For example, if you need to sort the search results by the sorting field defined by the form, you can use

where('name','like', $value . '%'); if (isset($data['sort'])) { $query->order($data['sort']); } } public function searchCreateTimeAttr($query, $value, $data) { $query->whereBetweenTime('create_time', $value[0], $value[1]); } }
Copy after login

Then, we can use the following query

User::withSearch(['name','create_time', 'status'], [ 'name'=>'think', 'create_time'=>['2018-8-1','2018-8-5'], 'status'=>1, 'sort'=>['status'=>'desc'], ]) ->select();
Copy after login

Final query SQL It may be

SELECT * FROM `think_user` WHERE `name` LIKE 'think%' AND `create_time` BETWEEN '2018-08-01 00:00:00' AND '2018-08-05 00:00:00' ORDER BY `status` DESC
Copy after login

You can also define field aliases for the searcher, for example:

User::withSearch(['name' => 'nickname','create_time', 'status'], [ 'nickname'=>'think', 'create_time'=>['2018-8-1','2018-8-5'], 'status'=>1, 'sort'=>['status'=>'desc'], ]) ->select();
Copy after login

The data searched uses the nickname field identifier, but we still use the searcher identified by the name field (That is, the searchNameAttr method).

The searcher is usually compared with the query range. No matter how many searchers are defined, it only needs to be called once. If the query range needs to be combined, the query needs to be called multiple times.

If you are using the Db query method, you can still use the search function, but the search method definition needs to be changed to a closure method, as follows:

User::withSearch(['name' => function($query,$value,$data){ $query->where('name','like', $value . '%'); }, 'create_time'=>function($query,$value,$data){ $query->whereBetweenTime('create_time', $value[0], $value[1]); }], [ 'name'=>'think', 'create_time'=>['2018-8-1','2018-8-5'], 'status'=>1 ]) ->select();
Copy after login

PHP Chinese website, there are a lot of free TheThinkPHP introductory tutorial, everyone is welcome to learn!

This article is reproduced from: https://blog.thinkphp.cn/783775

The above is the detailed content of ThinkPHP: One of the three most powerful tools for models (searcher). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:thinkphp.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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!