load()與save()怎么在Yii2中使用-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關load()與save()怎么在Yii 2中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都做網(wǎng)站、網(wǎng)站設計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的威縣網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎ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ù) 第二個參數(shù) 是一個空字符串 '';
//  $this->formName() 返回的額是 你實例化的類的名字 new test() 最后返回的是test

  if ($scope === '' && !empty($data)) {
   $this->setAttributes($data);  //進入

   return true;
  } elseif (isset($data[$scope])) {
   $this->setAttributes($data[$scope]);

   return true;
  } else {
   return false;
  }
 }

接下來看   setAttributes()

public function setAttributes($values, $safeOnly = true)
 {
  if (is_array($values)) {
   $attributes = array_flip($safeOnly ? $this->safeAttributes() : $this->attributes());
//這里執(zhí)行的是$this->safeAttributes()方法,該方法返回的是當前場景下需要驗證的字段。最后$attributes打印下來看下圖
foreach ($values as $name => $value) {
 if (isset($attributes[$name])) { 
    $this->$name = $value; 
 } elseif ($safeOnly) {
     $this->onUnsafeAttribute($name, $value); 
  } } 
 } 
}

圖: 圖1的文件名是test  實例化后是$test 對象 public function attribues()方法中對應的就是表字段。

我這里沒有用場景  所以暫時不講解場景這個功能。 不過大家可以看手冊。很容易懂。

load()與save()怎么在Yii 2中使用load()與save()怎么在Yii 2中使用

這兩個圖是對相應的

在之后 執(zhí)行的是 foreache循環(huán)  這里的$this 是那個$test 這個對象對象去調(diào)用

//例如post 提交過來的數(shù)據(jù)是這樣

$post=[

'a'=>123456,

'b'=>'abcdef'

] 

$test->a=123456

$test->b='abcdef'

所以這個load()方法只是分配post  或者get 發(fā)過來的數(shù)據(jù),不做驗證。

接下來看save();

查看save方法 。

public function save($runValidation = true, $attributeNames = null)
 {
  if ($this->getIsNewRecord()) {  //判斷是否是新紀錄
   return $this->insert($runValidation, $attributeNames);  //執(zhí)行這里 之后$this代表的是test 這個模型表。
                        //test 繼承的是\yii\mongodb\ActiveRecord 查看insert() 方法 。
    } else {
     return $this->update($runValidation, $attributeNames) !== false; }
   }

insert() 方法中

public function insert($runValidation = true, $attributes = null)
 {
  if ($runValidation && !$this->validate($attributes)) {    //下面的代碼分析validate方法 驗證rules
   return false;
  }
  $result = $this->insertInternal($attributes);  //保存數(shù)據(jù)

  return $result;
 }

首先看

//進行數(shù)據(jù)驗證。
public function validate($attributeNames = null, $clearErrors = true)
 {
  if ($clearErrors) {
   $this->clearErrors();
  }

  if (!$this->beforeValidate()) {  //在驗證之前首先執(zhí)行的是 beforValidata 
   return false;
  }

  $scenarios = $this->scenarios();    
  $scenario = $this->getScenario();    //檢查是否調(diào)用場景
  if (!isset($scenarios[$scenario])) {
   throw new InvalidParamException("Unknown scenario: $scenario");
  }

  if ($attributeNames === null) {
   $attributeNames = $this->activeAttributes(); //返回數(shù)組(值為屬性的名稱)
  }
//$this->getActiveValidators() 驗證數(shù)據(jù)。 讀取rules 方法 getActiveValidators() ->getValidators()->createValidators()這里驗證rules等信息->createValidator()
foreach ($this->getActiveValidators() as $validator) { 
      $validator->validateAttributes($this, $attributeNames); //獲取交集 檢查是否有錯誤 hasError()
     }
   $this->afterValidate();
   return !$this->hasErrors(); }

看完上述內(nèi)容,你們對load()與save()怎么在Yii 2中使用有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)站題目:load()與save()怎么在Yii2中使用-創(chuàng)新互聯(lián)
標題網(wǎng)址:http://muchs.cn/article42/cshsec.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、做網(wǎng)站、微信公眾號、Google、網(wǎng)站排名App設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設