Yii2.0 无法更新记录? [ 2.0 版本 ]
控制器action:
public function actionBasic() {
$model = new BasicForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$result = $model->basicSave($this->_userid);
die('保存成功!');
} else {
echo 'find';
$model = Seller::findOne($this->_userid);
return $this->render('basic', [
'model' => $model,
]);
}
}
Model:
<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
use frontend\models\Seller;
/**
* ContactForm is the model behind the contact form.
*/
class BasicForm extends Model
{
public $shop_name;
public $tel;
public $sign;
public $realname;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['shop_name', 'tel', 'sign', 'realname'], 'required'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
/*return [
'verifyCode' => 'Verification Code',
];*/
}
public function basicSave($userid) {
$user = Seller::findOne($userid);
$user->shop_name = $this->shop_name;
$user->tel = $this->tel;
$user->sign = $this->sign;
$user->realname = $this->realname;
//$user->generateAuthKey();
if ($user->save()) {
return $user;
}
return null;
}
}
在post的时候, 这句一直为false,哪里问题?
if ($model->load(Yii::$app->request->post()) && $model->validate())
共 5 个回答
-
在这行
if ($model->load(Yii::$app->request->post()) && $model->validate())
为false时,使用
var_dump($model->getErrors());
输出一下错误信息?
共 3 条回复public function basicSave($userid) { $user = Seller::findOne($userid); $user->shop_name = $this->shop_name; $user->tel = $this->tel; $user->sign = $this->sign; $user->realname = $this->realname; //$user->generateAuthKey(); if ($user->save()) { return $user; } return null; }
这里,
if($user->save())
为假时,输出一下var_dump($user->getErrors())
。
cfanbo
注册时间:2015-05-23
最后登录:2015-09-10
在线时长:11小时52分
最后登录:2015-09-10
在线时长:11小时52分
- 粉丝2
- 金钱110
- 威望0
- 积分220