Yii 2 - 无法使用模型更新

时间:2016-04-29 09:12:48

标签: php yii yii2

我有一些代码:

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

谢谢!

2 个答案:

答案 0 :(得分:0)

解决方案:

   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 :(得分:0)

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

你不能设置id:

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

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

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