mydzoo

mydzoo

这家伙有点懒,还没写个性签名!

  • 财富值2075
  • 威望值10
  • 总积分2705

个人信息

  • 2017-08-08 已签到
    连续签到2天,获得了10个金钱
  • 回复了 的回答

    “新建表单模型”的思路没问题。假设:

    • 表单模型为 PostForm, 内容大致如下:

      class PostForm extends \yii\base\Model
      {
          // 存储 Post 模型中的字段
          public $title; 
          // 存储 PostExtra 模型中的字段
          public $content;
          // 存储 PostAttachment 模型中的字段
          public $imgUrl;
        
          public function rules()
          {
              // ...
          }
      }
      
    • 一个 Post 对应一个 PostExtra;

    • 一个 Post 对应一个 PostAttachment;

    帖子修改操作的控制器代码大致如下:

    class PostController extends \yii\web\Controller
    {
        public function actionUpdate($id)
        {
            $post = Post::findOne($id);
            $extra = $post->extra;
            $attachment = $post->attachment;
    
            $postForm = new PostForm();
            $postForm->title = $post->title;
            $postForm->content = $extra->content;
            $postForm->imgUrl = $attachment->imgUrl;
    
            if ($postForm->load(Yii::$app->request->post()) && $postForm->validate()) {
                // 写入修改后的数据
            }
    
            return $this->render('update', ['postForm' => $postForm]);
        }
    }
    

    “多模型同时输入”也可以。你觉得“别扭”的三个 &&:

    • load() && load()
    • validate() && validate()
    • save() && save()

    前两个是必须的,最后一个可以放在一个事务内。过程大致如下:

    class PostController extends \yii\web\Controller
    {
        public function actionUpdate($id)
        {
            $post = Post::findOne($id);
            $extra = $post->extra;
            $attachment = $post->attachment;
    
            if (
                $post->load(Yii::$app->request->post()) 
                && $post->validate()
                && $extra->load(Yii::$app->request->post()) 
                && $extra->validate()
                && $attachment->load(Yii::$app->request->post()) 
                && $attachment->validate()
            ) {
                $transaction = Yii::$app->db->beginTransaction();
                try {
                    if (!$post->save(false)) {
                        throw new \yii\db\Exception('Failed to insert xxx.');
                    }
                    if (!$extra->save(false)) {
                        throw new \yii\db\Exception('Failed to insert xxx.');
                    }
                    if (!$attachment->save(false)) {
                        throw new \yii\db\Exception('Failed to insert xxx.');
                    }
            
                    $transaction->commit();
    
                    return $this->redirect('index');
    
                } catch(\Exception $e) {
                    $transaction->rollBack();
                    throw $e;
                }
            }
    
            return $this->render('update', [
                'post' => $post,
                'extra' => $extra,
                'attachment' => $attachment,
            ]);
        }
    }
    

    逆了天的完美回答。

  • 发表了说说
    学习进行中,一步一个坎,一步一疑惑。。。
  • 提出了问题
    Yii2符合表单模型的疑问求解惑
  • 2017-08-07 已签到
    连续签到1天,获得了5个金钱
  • 赞了说说
    这几天会在新闻上看见"勿为言之不预也"这句话么。这两天的新闻,看来祖国是要动手教育阿三的节奏
  • 2017-08-04 已签到
    连续签到1天,获得了5个金钱
  • 2017-08-02 已签到
    连续签到1天,获得了5个金钱
  • 2017-07-30 已签到
    连续签到2天,获得了10个金钱
总监 等级规则
2705/5000
资料完整度
10/100
用户活跃度
0/100

Ta的关注

2

Ta的粉丝

4

Ta的访客

30