简体   繁体   中英

Can't update database in Yii 2

Yii docs say that updateAttributes bypasses validation and records are updated directly into the database. 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.

Update: I discovered that there is actually a validation error occurs even while using updateAttributes. Now the question is why is it so while Yii docs say it does not validate.

Update 2 : I was wrongly using $this-> getErrors(), I replaced $this with $model.

If you have problem with $model->save(); , Use:

$model->save(false)

This solves your problem.

If you use $model->save(); the filters is running that is not good for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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