search
HomePHP FrameworkThinkPHPIntroducing the relationship between thinkphp5.0 modifier and data completion and how to use 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('非法访问');
        }
    }

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();
    }

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'
//        ]);
    }
}
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返回的结果】

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'
        ]);
    }
}

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"】
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'
]

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"【加密结果】

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'
        ]);
    }
}
After execution, the encryption is NULL
数据完成:在数据字段insert,update,auto时进行处理

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

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

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

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!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),