2016-01-23 54 views
0

Yii文檔說updateAttributes繞過了驗證,記錄直接更新到數據庫中。 在我的模型:無法更新Yii中的數據庫2

use yii\base\Component; 
use frontend\models\Notifications; 

class Notify extends \yii\db\ActiveRecord 
{ 


    public function send($user, $text){ 
     $model = new Notifications(); 
     if ($model->updateAttributes(['created_on'=>'2015-11-12 12:12:15', 'user_id'=>$user, 'text'=>$text])) return true; 
     else return $this->getErrors(); 
    } 
    public function notify($users, $text){ 

     $arr[] = array(); 
     foreach ($users as $user){ 
      $result = $this->send($user, $text); 
      $arr[]= $result; 

     } 
    return $arr; 
    } 
} 

在我的控制器

$notify = new Notify; 
$response = $notify->notify([1, $guardian], $note); 
var_dump($response); 

現在的問題是,無論是更新數據庫,也沒有顯示有任何錯誤。

我試着靜態調用方法,但是之後數據庫沒有更新。 我試着調用$ model-> save()..但仍然沒有更新數據庫。

更新: 我發現即使在使用updateAttributes時也會出現驗證錯誤。現在問題是爲什麼Yii文檔說它沒有驗證。

更新2: 我錯誤地使用$ this-> getErrors(),我用$ model替換$ this。

+1

你試過'$ model-> save(false)'?它將跳過其他字段驗證。 –

+0

是的,它的工作原理。謝謝。 –

回答

0

如果你有問題,$model->save();,用途:

$model->save(false) 

這可以解決你的問題。

如果您使用$model->save();過濾器正在運行,對您不利。