Yii 2 – 无法使用模型更新


Yii 2 – Can't use Model Update

我有一些代码:

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,
        ]);
    }
}

此代码工作不正确 - 它不会更新 MySQL 中的记录。在/runtime/debug日志中,我发现了有趣的时刻:

"UPDATE `SiteBlogPosts` SET `id`=5, `DateCreated`='2015-06-11', `Author`=1,
  `Title`='We are improved build mode!',
  `PreviewText`='<div class='"post-content'">'r'n<p>Hi.</p>'r'n'r'n<p></p>'r'n'r'n<p>We are improved build mode! Also, we added simple Mirror Material Test.</p>'r'n'r'n<p><iframe height='"350'" src='"http://www.youtube.com/embed/FLQ4i_av7HM'" width='"425'"></iframe></p>'r'n</div>'r'n',
  `FullTextOfPost`='<div class='"post-content'">'r'n<p>Hi.</p>'r'n'r'n<p></p>'r'n'r'n<p>We are improved build mode! Also, we added simple Mirror Material Test.</p>'r'n'r'n<p><iframe height='"350'" src='"http://www.youtube.com/embed/FLQ4i_av7HM'" width='"425'"></iframe></p>'r'n</div>'r'n',
  `CategoryID`=1 
WHERE `id` IS NULL"

否则,print_r显示:

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>

为什么它使用WHERE id IS NULL而不是WHERE id = 5

谢谢!

解决方案:

   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,
            ]);
        }
    }

Yii2 ActiveRecord 默认使用表 ID 作为主键。

您不能设置 ID :

  $model->id = $post['id'];

如果您有其他主键,则可以在站点博客文章模型中使用以下代码并更改 yii2 默认主列。

public static function primaryKey()
{
    return "your_column";
}