提示创建文章失败
PostForm表单模型的create()方法爆出“文章保存失败”的错误信息!
代码:
public function create()
{
$transaction = Yii::$app->db->beginTransaction();
try {
$model = new PostModel();
$model->setAttributes($this->attributes);
$model->summary = $this->_getSummary();
$model->user_id = Yii::$app->user->identity->id;
$model->user_name = Yii::$app->user->identity->username;
$model->is_valid = PostModel::IS_VALID;
$model->created_at = time();
$model->updated_at = time();
if($model->save()) {
throw new \Exception("文章保存失败!");
}
$this->id = $model->id;
$this->_eventAfterCreate();
$transaction->commit();
return true;
} catch (\Exception $e) {
$transaction->rollBack();
$this->_lastError = $e->getMessage();
return false;
}
}
$model 提交时的数据如下:
common\models\Posts Object ( [_attributes:yii\db\BaseActiveRecord:private] => Array ( [title] => 测试标题 [summary] => 测试内容 [content] =>
测试内容
[label_img] => /image/20170607/1496826479762736.jpg [cat_id] => 1 [user_id] => 560 [user_name] => test [is_valid] => 1 [created_at] => 1496839535 [updated_at] => 1496839535 ) [_oldAttributes:yii\db\BaseActiveRecord:private] => [_related:yii\db\BaseActiveRecord:private] => Array ( ) [_errors:yii\base\Model:private] => [_validators:yii\base\Model:private] => ArrayObject Object ( [storage:ArrayObject:private] => Array ( [0] => yii\validators\StringValidator Object ( [length] => [max] => [min] => [message] => {attribute}必须是一条字符串。 [tooShort] => [tooLong] => [notEqual] => [encoding] => UTF-8 [attributes] => Array ( [0] => content ) [on] => Array ( ) [except] => Array ( ) [skipOnError] => 1 [skipOnEmpty] => 1 [enableClientValidation] => 1 [isEmpty] => [when] => [whenClient] => [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => ) [1] => yii\validators\NumberValidator Object ( [integerOnly] => 1 [max] => [min] => [tooBig] => [tooSmall] => [integerPattern] => /^\s*[+-]?\d+\s*$/ [numberPattern] => /^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/ [attributes] => Array ( [0] => cat_id [1] => user_id [2] => is_valid [3] => created_at [4] => updated_at ) [message] => {attribute}必须是整数。 [on] => Array ( ) [except] => Array ( ) [skipOnError] => 1 [skipOnEmpty] => 1 [enableClientValidation] => 1 [isEmpty] => [when] => [whenClient] => [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => ) [2] => yii\validators\StringValidator Object ( [length] => [max] => 255 [min] => [message] => {attribute}必须是一条字符串。 [tooShort] => [tooLong] => {attribute}只能包含至多{max, number}个字符。 [notEqual] => [encoding] => UTF-8 [attributes] => Array ( [0] => title [1] => summary [2] => label_img [3] => user_name ) [on] => Array ( ) [except] => Array ( ) [skipOnError] => 1 [skipOnEmpty] => 1 [enableClientValidation] => 1 [isEmpty] => [when] => [whenClient] => [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => ) ) ) [_scenario:yii\base\Model:private] => default [_events:yii\base\Component:private] => Array ( ) [_behaviors:yii\base\Component:private] => Array ( ) )