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

<?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]);
    }    
}
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([&#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();
Copy after login

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

<?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]);
    }      
}
Copy after login

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();
Copy after login

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
Copy after login

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();
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([&#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();
Copy after login

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!

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
Popular Tutorials
More>
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!