dryb38654 2016-04-29 09:12
浏览 28
已采纳

Yii 2 - 无法使用模型更新

I have some code:

public function actionEditpost($id) {
    $post = SiteBlogPosts::find()->
            where(["id" => $id])->
            asArray()->
            one();

    $model = new SiteBlogPosts();
    $model->id = $post['id'];
    $model->DateCreated = $post['DateCreated'];
    $model->Author = $post['Author'];
    $model->Title = $post['Title'];
    $model->PreviewText = $post['PreviewText'];
    $model->FullTextOfPost = $post['FullTextOfPost'];
    $model->CategoryID = $post['CategoryID'];

    if ($model->load(Yii::$app->request->post())) {

        if ($model->validate()) {

            $model->update();
            print_r($model);
            die;
            return $this->render('AddPostDone', ['model' => $model]);
        }
    } else {
        $cats = SiteBlogCats::find()->where(["CatIsActive" => 1])->asArray()->all();
        return $this->render('EditPost', ['cats' => $cats,
                    "model" => $model,
                    'oldPost' => $post,
        ]);
    }
}

This code works incorrectly - it doesn't update record in MySQL. In /runtime/debug logs I found interesting moment:

"UPDATE `SiteBlogPosts` SET `id`=5, `DateCreated`='2015-06-11', `Author`=1,
  `Title`='We are improved build mode!',
  `PreviewText`='<div class=\"post-content\">
<p>Hi.</p>

<p></p>

<p>We are improved build mode! Also, we added simple Mirror Material Test.</p>

<p><iframe height=\"350\" src=\"http://www.youtube.com/embed/FLQ4i_av7HM\" width=\"425\"></iframe></p>
</div>
',
  `FullTextOfPost`='<div class=\"post-content\">
<p>Hi.</p>

<p></p>

<p>We are improved build mode! Also, we added simple Mirror Material Test.</p>

<p><iframe height=\"350\" src=\"http://www.youtube.com/embed/FLQ4i_av7HM\" width=\"425\"></iframe></p>
</div>
',
  `CategoryID`=1 
WHERE `id` IS NULL"

Otherwise, print_r shows:

app\models\SiteBlogPosts Object
(
    [sqlCreateQuery] => 
    [_attributes:yii\db\BaseActiveRecord:private] => Array
        (
            [id] => 5
            [DateCreated] => 2015-06-11
            [Author] => 1
            [Title] => We are improved build mode!
            [PreviewText] => <div class="post-content">
<p>Hi.</p>

<p></p>

<p>We are improved build mode! Also, we added simple Mirror Material Test.</p>

<p><iframe height="350" src="http://www.youtube.com/embed/FLQ4i_av7HM" width="425"></iframe></p>
</div>

Why it used WHERE id IS NULL instead of WHERE id = 5?

Thank you!

  • 写回答

2条回答 默认 最新

  • douhan9467 2016-04-29 09:24
    关注

    Solution:

       public function actionEditpost($id) {
            $model = SiteBlogPosts::find()->
                    where(["id" => $id])->
                    one();
    
            $cats = SiteBlogCats::find()->where(["CatIsActive" => 1])->asArray()->all();
    
    
            if ($model->load(Yii::$app->request->post())) {
    
                if ($model->validate()) {
                    $model->update();
    //                print_r($model);
    //                die;
                    return $this->render('EditPost', ['cats' => $cats,
                                "model" => $model,
                    ]);
                }
            } else {
                return $this->render('EditPost', ['cats' => $cats,
                            "model" => $model,
                ]);
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分