久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      介紹thinkphp5.0修改器和數(shù)據(jù)完成的關(guān)系及使用方法

      下面由thinkphp教程欄目給大家介紹thinkphp5.0修改器和數(shù)據(jù)完成的關(guān)系及使用方法,希望對(duì)需要的朋友有所幫助!

      thinkphp5.0修改器和數(shù)據(jù)完成的關(guān)系以及使用方法

      密碼加密時(shí)遇到的問(wèn)題

      今天遇到密碼md5加密的問(wèn)題,當(dāng)時(shí)使用的是 "thinkphp5.0.9->模型->數(shù)據(jù)完成" 實(shí)現(xiàn)的自動(dòng)進(jìn)行加密,但是在上面 "thinkphp5.0.9->模型->修改器" 中發(fā)現(xiàn)修改器和數(shù)據(jù)完成功能一樣,看下方的評(píng)論說(shuō)是數(shù)據(jù)完成和修改器配合使用,我就照著做,當(dāng)時(shí)這樣寫(xiě)的:

      //模型層  class User extends Model{ //$auto包含新增$insert和更新操作$update,就是不管新增還是更新我就自動(dòng)執(zhí)行     protected $auto = ['password','create'];     public function setPasswordAttr($value)     {         return md5($value);     }     public function setCreateAttr()     {         return time();     }      //注冊(cè)用戶(hù)     public function register($data){             $bool = $this->save($data);             return $bool ? $this->id : 0;     }  }  //控制器層方法 public function register()     {         if(request()->isAjax()){             $userModel=new appindexModelUser();             $data=input('post.'); //注冊(cè)             $res = $userModel->register($data);            echo $res;         }else{             $this->error('非法訪問(wèn)');         }     }

      介紹thinkphp5.0修改器和數(shù)據(jù)完成的關(guān)系及使用方法

      我輸入 "wwwwww" 按照上面的代碼進(jìn)行注冊(cè)后password加密結(jié)果是b8d3c8f4db0c248ac242dd6e098bbf85

      正確的加密結(jié)果是 d785c99d298a4e9e6e13fe99e602ef42,這個(gè)時(shí)候你可能沒(méi)發(fā)現(xiàn),當(dāng)你登陸的時(shí)候就是登陸不上去,你肯定再去注冊(cè)一個(gè)新用戶(hù),比如密碼還是wwwwww,你登陸的時(shí)候還是登陸不上去,只能懷疑加密出錯(cuò),再往上找到了 "數(shù)據(jù)完成的setPasswordAttr()"

      單獨(dú)拿出來(lái)測(cè)試

      直接說(shuō)答案吧,我當(dāng)時(shí)看了多遍修改器和數(shù)據(jù)完成測(cè)試兩個(gè)小時(shí)終于知道原因了,新建的test表

      介紹thinkphp5.0修改器和數(shù)據(jù)完成的關(guān)系及使用方法

      //新建test模型層 namespace appindexModel; use thinkModel; 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 "數(shù)據(jù)完成";         $this->save([             'username'  => 'thinkphp',             'password'  => 'wwwwww',             'create'    => '123456'         ]);     } }  //控制器中添加test方法  public function test(){         $user = model('Test');         //調(diào)用model層函數(shù)         $user->addPass();     }

      單獨(dú)測(cè)試修改器

      首先注釋掉模型層中的 “數(shù)據(jù)完成” 部分

      namespace appindexModel; use thinkModel; class Test extends Model {     protected $auto = ['password'];     protected function setPasswordAttr($value)     {         dump(md5(NULL));//把NULL加密         dump($value);   //查看調(diào)用時(shí)傳遞過(guò)來(lái)的值         dump(md5($value));//把該值加密         return md5($value);//把該值加密返回      }     public function addPass(){         echo "修改器:修改器的作用是可以在數(shù)據(jù)賦值的時(shí)候自動(dòng)進(jìn)行轉(zhuǎn)換處理";         $this->password='wwwwww';         dump($this->password);//輸出返回后的結(jié)果  //        echo "數(shù)據(jù)完成:在數(shù)據(jù)字段insert,update,auto時(shí)進(jìn)行處理"; //        $this->save([ //            'username'  => 'thinkphp', //            'password'  => 'wwwwww', //            'create'    => '123456' //        ]);     } }

      執(zhí)行后頁(yè)面顯示結(jié)果,通過(guò)結(jié)果發(fā)現(xiàn)修改器是在賦值的時(shí)候執(zhí)行的自動(dòng)加密,注意:此時(shí)并沒(méi)有存入數(shù)據(jù)庫(kù)!

      修改器:修改器的作用是可以在數(shù)據(jù)賦值的時(shí)候自動(dòng)進(jìn)行轉(zhuǎn)換處理  string(32) "d41d8cd98f00b204e9800998ecf8427e"【加密的NULL】  string(6) "wwwwww"【傳過(guò)來(lái)的$value】  string(32) "d785c99d298a4e9e6e13fe99e602ef42"【加密$value】  string(32) "d785c99d298a4e9e6e13fe99e602ef42"【return返回的結(jié)果】

      測(cè)試數(shù)據(jù)完成

      注釋掉“修改器”部分的代碼,僅執(zhí)行數(shù)據(jù)完成

      namespace appindexModel; use thinkModel; class Test extends Model {     protected $auto = ['password'];     protected function setPasswordAttr($value)     {         dump(md5(NULL));//把NULL加密         dump($value);   //查看調(diào)用時(shí)傳遞過(guò)來(lái)的值         dump(md5($value));//把該值加密         return md5($value);//把該值加密返回      }     public function addPass(){ //        echo "修改器:修改器的作用是可以在數(shù)據(jù)賦值的時(shí)候自動(dòng)進(jìn)行轉(zhuǎn)換處理"; //        $this->password='wwwwww'; //        dump($this->password);//輸出返回后的結(jié)果          echo "數(shù)據(jù)完成:在數(shù)據(jù)字段insert,update,auto時(shí)進(jìn)行處理";         $this->save([             'username'  => 'thinkphp',             'password'  => 'wwwwww',             'create'    => '123456'         ]);     } }

      找到原因

      執(zhí)行后發(fā)現(xiàn)setPasswordAttr()被執(zhí)行了兩次,所以password也被加密了兩次;

      數(shù)據(jù)完成:在數(shù)據(jù)字段insert,update,auto時(shí)進(jìn)行處理  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"】

      加密兩次的原因是在賦值的時(shí)候加密一次,自動(dòng)完成$auto時(shí)加密了一次

      [     'username'  => 'thinkphp',     'password'  => 'wwwwww',     'create'    => '123456' ]

      解決開(kāi)始的問(wèn)題

      如果想要加密一次就把 protected $auto = ['password']; 注釋掉,或者在登陸的代碼中進(jìn)行md5(md5("wwwwww")),注釋掉后執(zhí)行:

      數(shù)據(jù)完成:在數(shù)據(jù)字段insert,update,auto時(shí)進(jìn)行處理  string(32) "d41d8cd98f00b204e9800998ecf8427e"【加密NULL】  string(6) "wwwwww"【$value】  string(32) "d785c99d298a4e9e6e13fe99e602ef42"【加密結(jié)果】

      介紹thinkphp5.0修改器和數(shù)據(jù)完成的關(guān)系及使用方法

      如果是多個(gè)字段protected $auto = ['password','create'];就把password去掉就可以了protected $auto = ['create'];,所以最開(kāi)始的問(wèn)題就解決了。

      當(dāng)只有數(shù)據(jù)完成但不賦值

      在上面可能注意到我怎么老是加密 NULL 干什么,還有另一種情況就是 protected $auto = ['password']; 定義了自動(dòng)完成,但是我并沒(méi)有賦值:

      namespace appindexModel; use thinkModel; class Test extends Model {     protected $auto = ['password'];     protected function setPasswordAttr($value)     {         dump(md5(NULL));//把NULL加密         dump($value);   //查看調(diào)用時(shí)傳遞過(guò)來(lái)的值         dump(md5($value));//把該值加密         return md5($value);//把該值加密返回      }     public function addPass(){ //        echo "修改器:修改器的作用是可以在數(shù)據(jù)賦值的時(shí)候自動(dòng)進(jìn)行轉(zhuǎn)換處理"; //        $this->password='wwwwww'; //        dump($this->password);//輸出返回后的結(jié)果          echo "數(shù)據(jù)完成:在數(shù)據(jù)字段insert,update,auto時(shí)進(jìn)行處理";         $this->save([             'username'  => 'thinkphp', //注釋掉,不賦值  //           'password'  => 'wwwwww',             'create'    => '123456'         ]);     } }

      執(zhí)行后,加密的是 NULL

      數(shù)據(jù)完成:在數(shù)據(jù)字段insert,update,auto時(shí)進(jìn)行處理  string(32) "d41d8cd98f00b204e9800998ecf8427e"【加密NULL】  NULL【沒(méi)有傳值,$value=NULL】  string(32) "d41d8cd98f00b204e9800998ecf8427e"【加密$value,剛好等于NULL加密結(jié)果】

      介紹thinkphp5.0修改器和數(shù)據(jù)完成的關(guān)系及使用方法

      剩下的$update和$insert使用方法同$auto一樣,$auto包含$update和$insert

      總結(jié)

      修改器會(huì)在賦值時(shí)執(zhí)行;數(shù)據(jù)完成會(huì)被執(zhí)行兩次,一次是賦值時(shí),一次是寫(xiě)入數(shù)據(jù)時(shí)

      希望手冊(cè)能稍微詳細(xì)一點(diǎn)點(diǎn),白白耽誤我開(kāi)發(fā)時(shí)間,特此分享,大家少踩坑,如果理解的不對(duì)請(qǐng)指正,謝謝

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)