[√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:
<?php namespace app\index\model; use think\Model; class User extends Model { public function searchNameAttr($query, $value, $data) { $query->where('name','like', $value . '%'); } public function searchCreateTimeAttr($query, $value, $data) { $query->whereBetweenTime('create_time', $value[0], $value[1]); } }
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();
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'
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
<?php namespace app\index\model; use think\Model; class User extends Model { public function searchNameAttr($query, $value, $data) { $query->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]); } }
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();
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
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();
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();
PHP Chinese website, there are a lot of free The ThinkPHP 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!