Home > PHP Framework > ThinkPHP > body text

Introducing the relationship between thinkphp5.0 modifier and data completion and how to use it

藏色散人
Release: 2021-04-29 09:01:16
forward
1763 people have browsed it

The following tutorial column of thinkphp will introduce to you the relationship between thinkphp5.0 modifier and data completion and how to use it. I hope it will be helpful to friends in need!

The relationship between thinkphp5.0 modifier and data completion and how to use it

Problems encountered when password encryption

Today I encountered the problem of password md5 encryption, At that time, "thinkphp5.0.9->Model->Data Completion" was used to implement automatic encryption, but in the "thinkphp5.0.9->Model->Modifier" above, it was found that the modifier has the same function as the data completion , read the comments below that it is used in conjunction with data completion and modifiers, so I followed it and wrote like this:
//模型层

class User extends Model{
//$auto包含新增$insert和更新操作$update,就是不管新增还是更新我就自动执行
    protected $auto = ['password','create'];
    public function setPasswordAttr($value)
    {
        return md5($value);
    }
    public function setCreateAttr()
    {
        return time();
    }
    
//注册用户
    public function register($data){
            $bool = $this->save($data);
            return $bool ? $this->id : 0;
    } 
}

//控制器层方法
public function register()
    {
        if(request()->isAjax()){
            $userModel=new \app\index\Model\User();
            $data=input('post.');
//注册
            $res = $userModel->register($data);
           echo $res;
        }else{
            $this->error('非法访问');
        }
    }
Copy after login

Introducing the relationship between thinkphp5.0 modifier and data completion and how to use it

I entered "wwwwww" according to the above After the code is registered, the password encryption result is b8d3c8f4db0c248ac242dd6e098bbf85

The correct encryption result is d785c99d298a4e9e6e13fe99e602ef42. You may not notice it at this time. When you log in, you cannot log in. You must register a new user. For example, the password is still wwwwww. When you log in, you still can't log in. You can only suspect that there is an encryption error. Then you find "setPasswordAttr() with data completed"

Take it out separately and test it

Just tell me the answer Well, I looked at the modifiers and data multiple times and completed the test for two hours and finally found out the reason. The newly created test table

Introducing the relationship between thinkphp5.0 modifier and data completion and how to use it

//新建test模型层
namespace app\index\Model;
use think\Model;
class Test extends Model
{
    protected $auto = ['password'];
    protected function setPasswordAttr($value)
    {
        dump(md5(NULL));
        dump($value);
        dump(md5($value));
        return md5($value);

    }
    public function addPass(){
        echo "修改器";
        $this->password='wwwwww';
        dump($this->password);
        
        echo "数据完成";
        $this->save([
            'username'  => 'thinkphp',
            'password'  => 'wwwwww',
            'create'    => '123456'
        ]);
    }
}

//控制器中添加test方法
 public function test(){
        $user = model('Test');
        //调用model层函数
        $user->addPass();
    }
Copy after login

tested the modifiers alone

First comment out the "data completion" part in the model layer
namespace app\index\Model;
use think\Model;
class Test extends Model
{
    protected $auto = ['password'];
    protected function setPasswordAttr($value)
    {
        dump(md5(NULL));//把NULL加密
        dump($value);   //查看调用时传递过来的值
        dump(md5($value));//把该值加密
        return md5($value);//把该值加密返回

    }
    public function addPass(){
        echo "修改器:修改器的作用是可以在数据赋值的时候自动进行转换处理";
        $this->password='wwwwww';
        dump($this->password);//输出返回后的结果

//        echo "数据完成:在数据字段insert,update,auto时进行处理";
//        $this->save([
//            'username'  => 'thinkphp',
//            'password'  => 'wwwwww',
//            'create'    => '123456'
//        ]);
    }
}
Copy after login
After execution, the page displays the results. Through the results, it is found that the modifier is automatically encrypted when assigning values. Note: it is not stored at this time. database!
修改器:修改器的作用是可以在数据赋值的时候自动进行转换处理

string(32) "d41d8cd98f00b204e9800998ecf8427e"【加密的NULL】

string(6) "wwwwww"【传过来的$value】

string(32) "d785c99d298a4e9e6e13fe99e602ef42"【加密$value】

string(32) "d785c99d298a4e9e6e13fe99e602ef42"【return返回的结果】
Copy after login

Test data completion

Comment out the code in the "modifier" part and only execute the data completion
namespace app\index\Model;
use think\Model;
class Test extends Model
{
    protected $auto = ['password'];
    protected function setPasswordAttr($value)
    {
        dump(md5(NULL));//把NULL加密
        dump($value);   //查看调用时传递过来的值
        dump(md5($value));//把该值加密
        return md5($value);//把该值加密返回

    }
    public function addPass(){
//        echo "修改器:修改器的作用是可以在数据赋值的时候自动进行转换处理";
//        $this->password='wwwwww';
//        dump($this->password);//输出返回后的结果

        echo "数据完成:在数据字段insert,update,auto时进行处理";
        $this->save([
            'username'  => 'thinkphp',
            'password'  => 'wwwwww',
            'create'    => '123456'
        ]);
    }
}
Copy after login

Find the reason

After executing setPasswordAttr( ) is executed twice, so the password is also encrypted twice;
数据完成:在数据字段insert,update,auto时进行处理

string(32) "d41d8cd98f00b204e9800998ecf8427e"【加密NULL】

string(6) "wwwwww"【传入的$value】

string(32) "d785c99d298a4e9e6e13fe99e602ef42"【加密$value="wwwwww"】

string(32) "d41d8cd98f00b204e9800998ecf8427e"【加密NULL】

string(32) "d785c99d298a4e9e6e13fe99e602ef42"【传入的$value】

string(32) "b8d3c8f4db0c248ac242dd6e098bbf85"【再次加密$value="d785c99...f42"】
Copy after login
The reason why it is encrypted twice is that it is encrypted once when assigning a value and once when $auto is automatically completed
[
    'username'  => 'thinkphp',
    'password'  => 'wwwwww',
    'create'    => '123456'
]
Copy after login

Solving the initial problem

If you want to encrypt once, comment out protected $auto = ['password'];, or perform md5(md5("wwwwww")) in the login code and comment out After execution:
数据完成:在数据字段insert,update,auto时进行处理

string(32) "d41d8cd98f00b204e9800998ecf8427e"【加密NULL】

string(6) "wwwwww"【$value】

string(32) "d785c99d298a4e9e6e13fe99e602ef42"【加密结果】
Copy after login

Introducing the relationship between thinkphp5.0 modifier and data completion and how to use it

If there are multiple fields protected $auto = ['password','create']; just remove the password protected $auto = [ 'create'];, so the original problem is solved.

When only the data is completed but no value is assigned

You may have noticed above how I always encrypt NULL. There is another situation where protected $auto = ['password']; definition Automatically completed, but I did not assign a value:
namespace app\index\Model;
use think\Model;
class Test extends Model
{
    protected $auto = ['password'];
    protected function setPasswordAttr($value)
    {
        dump(md5(NULL));//把NULL加密
        dump($value);   //查看调用时传递过来的值
        dump(md5($value));//把该值加密
        return md5($value);//把该值加密返回

    }
    public function addPass(){
//        echo "修改器:修改器的作用是可以在数据赋值的时候自动进行转换处理";
//        $this->password='wwwwww';
//        dump($this->password);//输出返回后的结果

        echo "数据完成:在数据字段insert,update,auto时进行处理";
        $this->save([
            'username'  => 'thinkphp',
//注释掉,不赋值
 //           'password'  => 'wwwwww',
            'create'    => '123456'
        ]);
    }
}
Copy after login
After execution, the encryption is NULL
数据完成:在数据字段insert,update,auto时进行处理

string(32) "d41d8cd98f00b204e9800998ecf8427e"【加密NULL】

NULL【没有传值,$value=NULL】

string(32) "d41d8cd98f00b204e9800998ecf8427e"【加密$value,刚好等于NULL加密结果】
Copy after login

Introducing the relationship between thinkphp5.0 modifier and data completion and how to use it

The remaining $update and $insert usage methods Like $auto, $auto includes $update and $insert

Summary

The modifier will be executed when assigning; data completion will be executed twice, once when assigning and once when writing When entering data
I hope the manual can be a little more detailed, as it wastes my development time. I would like to share this with you so that everyone can avoid pitfalls. Please correct me if you understand something wrong, thank you

The above is the detailed content of Introducing the relationship between thinkphp5.0 modifier and data completion and how to use it. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:segmentfault.com
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