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!