Table of Contents
Problems encountered when password encryption
Take it out separately and test it
tested the modifiers alone
Test data completion
Find the reason
Solving the initial problem
When only the data is completed but no value is assigned
Summary
Home PHP Framework ThinkPHP Introducing the relationship between thinkphp5.0 modifier and data completion and how to use it

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

Apr 27, 2021 pm 04:39 PM
php

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!

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles