《PHP實(shí)例:Yii 2中的load()和save()示例詳解》要點(diǎn):
本文介紹了PHP實(shí)例:Yii 2中的load()和save()示例詳解,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
前言PHP應(yīng)用
本文主要給大家介紹的是關(guān)于Yii2中l(wèi)oad()和save()的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),話不多說,來一起看看詳細(xì)的介紹吧.PHP應(yīng)用
我這里用的數(shù)據(jù)庫是mongo 數(shù)據(jù)庫 為栗子:PHP應(yīng)用
public function load($data, $formName = null) { $scope = $formName === null ? $this->formName() : $formName; //調(diào)用load 一般我是 $test = new test() $test->load('參數(shù)1','參數(shù)2') // 參數(shù)1 一般是post get 傳過來的參數(shù) 第二個(gè)參數(shù) 是一個(gè)空字符串 ''; // $this->formName() 返回的額是 你實(shí)例化的類的名字 new test() 最后返回的是test if ($scope === '' && !empty($data)) { $this->setAttributes($data); //進(jìn)入 return true; } elseif (isset($data[$scope])) { $this->setAttributes($data[$scope]); return true; } else { return false; } }
接下來看?? setAttributes()PHP應(yīng)用
public function setAttributes($values, $safeOnly = true) { if (is_array($values)) { $attributes = array_flip($safeOnly ? $this->safeAttributes() : $this->attributes()); //這里執(zhí)行的是$this->safeAttributes()方法,該方法返回的是當(dāng)前場(chǎng)景下需要驗(yàn)證的字段.最后$attributes打印下來看下圖 foreach ($values as $name => $value) { if (isset($attributes[$name])) { $this->$name = $value; } elseif ($safeOnly) { $this->onUnsafeAttribute($name, $value); } } } }
圖: 圖1的文件名是test? 實(shí)例化后是$test 對(duì)象? public function attribues()
方法中對(duì)應(yīng)的就是表字段.PHP應(yīng)用
我這里沒有用場(chǎng)景? 所以暫時(shí)不講解場(chǎng)景這個(gè)功能. 不過大家可以看手冊(cè).很容易懂.PHP應(yīng)用
PHP應(yīng)用
這兩個(gè)圖是對(duì)相應(yīng)的PHP應(yīng)用
在之后 執(zhí)行的是 foreache循環(huán)? 這里的$this 是那個(gè)$test 這個(gè)對(duì)象對(duì)象去調(diào)用PHP應(yīng)用
//例如post 提交過來的數(shù)據(jù)是這樣 $post=[ 'a'=>123456, 'b'=>'abcdef' ] $test->a=123456 $test->b='abcdef'
所以這個(gè)load()方法只是分配post? 或者get 發(fā)過來的數(shù)據(jù),不做驗(yàn)證.PHP應(yīng)用
接下來看save();PHP應(yīng)用
查看save方法 .PHP應(yīng)用
public function save($runValidation = true, $attributeNames = null) { if ($this->getIsNewRecord()) { //判斷是否是新紀(jì)錄 return $this->insert($runValidation, $attributeNames); //執(zhí)行這里 之后$this代表的是test 這個(gè)模型表. //test 繼承的是\yii\mongodb\ActiveRecord 查看insert() 方法 . } else { return $this->update($runValidation, $attributeNames) !== false; } }
insert() 方法中PHP應(yīng)用
public function insert($runValidation = true, $attributes = null) { if ($runValidation && !$this->validate($attributes)) { //下面的代碼分析validate方法 驗(yàn)證rules return false; } $result = $this->insertInternal($attributes); //保存數(shù)據(jù) return $result; }
首先看PHP應(yīng)用
//進(jìn)行數(shù)據(jù)驗(yàn)證. public function validate($attributeNames = null, $clearErrors = true) { if ($clearErrors) { $this->clearErrors(); } if (!$this->beforeValidate()) { //在驗(yàn)證之前首先執(zhí)行的是 beforValidata return false; } $scenarios = $this->scenarios(); $scenario = $this->getScenario(); //檢查是否調(diào)用場(chǎng)景 if (!isset($scenarios[$scenario])) { throw new InvalidParamException("Unknown scenario: $scenario"); } if ($attributeNames === null) { $attributeNames = $this->activeAttributes(); //返回?cái)?shù)組(值為屬性的名稱) } //$this->getActiveValidators() 驗(yàn)證數(shù)據(jù). 讀取rules 方法 getActiveValidators() ->getValidators()->createValidators()這里驗(yàn)證rules等信息->createValidator() foreach ($this->getActiveValidators() as $validator) { $validator->validateAttributes($this, $attributeNames); //獲取交集 檢查是否有錯(cuò)誤 hasError() } $this->afterValidate(); return !$this->hasErrors(); }
此時(shí)數(shù)據(jù)驗(yàn)證完畢,之后就保存數(shù)據(jù) 保存數(shù)據(jù)這塊 就暫時(shí)不寫了 后續(xù)補(bǔ)上.PHP應(yīng)用
好了 其中數(shù)據(jù)驗(yàn)證那一塊 大家多看看源碼吧. 一遍不行2遍 3遍 多了就懂了.PHP應(yīng)用
總結(jié)PHP應(yīng)用
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)維易PHP的支持.PHP應(yīng)用
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/381.html