[英]Can't update database in Yii 2
Yii文件说,updateAttributes绕过验证,记录直接更新到数据库中。 在我的模型中:
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。
如果您对$model->save();
有问题, , 采用:
$model->save(false)
这样可以解决您的问题。
如果使用$model->save();
筛选器正在运行,对您不利。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.