为了账号安全,请及时绑定邮箱和手机立即绑定

提示创建文章失败

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 ( ) )


正在回答

4 回答

他讲的一般,去这个  https://github.com/pcyanglei/yii2-qiniu-images

0 回复 有任何疑惑可以回复我~
      if($model->save()) {
        throw new \Exception("文章保存失败!");

if() 括号里少了 !

0 回复 有任何疑惑可以回复我~

各位高手指点,难道是哪里的代码,或者环境有问题?


另,$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 ( ) )


0 回复 有任何疑惑可以回复我~
#1

几分从前

怎么解决的 我的也是
2018-08-21 回复 有任何疑惑可以回复我~

MySQL版本 5.5.34,支持InnoDB,且确认过默认开启了对事务的支持;也检查确认了数据表posts情况,采用的也是InnoDB。

*************************** 1. row ***************************

           Name: posts

         Engine: InnoDB

        Version: 10

     Row_format: Compact

           Rows: 1

 Avg_row_length: 16384

    Data_length: 16384

Max_data_length: 0

   Index_length: 16384

      Data_free: 1126170624

 Auto_increment: 57

    Create_time: 2017-06-06 21:58:56

    Update_time: NULL

     Check_time: NULL

      Collation: utf8_general_ci

       Checksum: NULL

 Create_options: 

        Comment: 文章主表

1 row in set (0.00 sec)


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
PHP之Yii2框架搭建完整博客系统
  • 参与学习       22577    人
  • 解答问题       291    个

Yii是PHP快速开发的最佳实践之一,一起领略yii2快速开发的风采

进入课程

提示创建文章失败

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号