简体   繁体   English

无法在Yii 2中更新数据库

[英]Can't update database in Yii 2

Yii docs say that updateAttributes bypasses validation and records are updated directly into the database. Yii文件说,updateAttributes绕过验证,记录直接更新到数据库中。 In my Model: 在我的模型中:

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;
    }
}

In my controller 在我的控制器中

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

Now the problem is that Neither the databases are updated nor it shows any error. 现在的问题是数据库既不更新也不显示任何错误。

I've tried calling the method statically but then again database is not updated. 我尝试过静态调用该方法,但是数据库又没有更新。 I've tried calling the $model->save().. but still the database is not updated. 我试过调用$ model-> save()..但仍然没有更新数据库。

Update: I discovered that there is actually a validation error occurs even while using updateAttributes. 更新:我发现即使使用updateAttributes时,实际上也会发生验证错误。 Now the question is why is it so while Yii docs say it does not validate. 现在的问题是,为什么当Yii文档说它没有通过验证时会如此。

Update 2 : I was wrongly using $this-> getErrors(), I replaced $this with $model. 更新2:我错误地使用$ this-> getErrors(),我用$ model代替了$ this。

If you have problem with $model->save(); 如果您对$model->save();有问题, , Use: , 采用:

$model->save(false)

This solves your problem. 这样可以解决您的问题。

If you use $model->save(); 如果使用$model->save(); the filters is running that is not good for you. 筛选器正在运行,对您不利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM