Home  >  Article  >  PHP Framework  >  ThinkPHP: One of the three most powerful tools for models (searcher)

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

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼forward
2019-12-16 16:51:503178browse

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:

<?php
namespace app\index\model;
use think\Model;
class User extends Model 
{
    public function searchNameAttr($query, $value, $data)
    {
        $query->where(&#39;name&#39;,&#39;like&#39;, $value . &#39;%&#39;);
    }
    
    public function searchCreateTimeAttr($query, $value, $data)
    {
        $query->whereBetweenTime(&#39;create_time&#39;, $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([&#39;name&#39;, &#39;create_time&#39;], [
&#39;name&#39;=>&#39;think&#39;,
    &#39;create_time&#39;=>[&#39;2018-8-1&#39;,&#39;2018-8-5&#39;],
        &#39;status&#39;=>1
    ])
->select();

The final generated SQL statement is similar to

SELECT * FROM `think_user` WHERE  `name` LIKE &#39;think%&#39; AND `create_time` BETWEEN &#39;2018-08-01 00:00:00&#39; AND 
&#39;2018-08-05 00:00:00&#39;

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(&#39;name&#39;,&#39;like&#39;, $value . &#39;%&#39;);
        if (isset($data[&#39;sort&#39;])) {
        $query->order($data[&#39;sort&#39;]);
        }        
    }
    
    public function searchCreateTimeAttr($query, $value, $data)
    {
        $query->whereBetweenTime(&#39;create_time&#39;, $value[0], $value[1]);
    }      
}

Then, we can use the following query

User::withSearch([&#39;name&#39;,&#39;create_time&#39;, &#39;status&#39;], [
&#39;name&#39;=>&#39;think&#39;,
    &#39;create_time&#39;=>[&#39;2018-8-1&#39;,&#39;2018-8-5&#39;],
        &#39;status&#39;=>1,
        &#39;sort&#39;=>[&#39;status&#39;=>&#39;desc&#39;],
    ])
->select();

Final query SQL It may be

SELECT * FROM `think_user` WHERE  `name` LIKE &#39;think%&#39; AND `create_time` BETWEEN &#39;2018-08-01 00:00:00&#39; AND 
&#39;2018-08-05 00:00:00&#39; ORDER BY `status` DESC

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

User::withSearch([&#39;name&#39; => &#39;nickname&#39;,&#39;create_time&#39;, &#39;status&#39;], [
&#39;nickname&#39;=>&#39;think&#39;,
    &#39;create_time&#39;=>[&#39;2018-8-1&#39;,&#39;2018-8-5&#39;],
        &#39;status&#39;=>1,
        &#39;sort&#39;=>[&#39;status&#39;=>&#39;desc&#39;],
    ])
->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([&#39;name&#39; => function($query,$value,$data){
    $query->where(&#39;name&#39;,&#39;like&#39;, $value . &#39;%&#39;);
}, &#39;create_time&#39;=>function($query,$value,$data){
    $query->whereBetweenTime(&#39;create_time&#39;, $value[0], $value[1]);
}], [
&#39;name&#39;=>&#39;think&#39;,
    &#39;create_time&#39;=>[&#39;2018-8-1&#39;,&#39;2018-8-5&#39;],
        &#39;status&#39;=>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!

Statement:
This article is reproduced at:thinkphp.cn. If there is any infringement, please contact admin@php.cn delete