Answer the question
In order to leave comments, you need to log in
Yii2 2 records are saved to the database at once. Why?
The story is like this. When I go to the /create page, an entry is automatically created in the database. Then it redirects me to /update. Where do I start working with this record.
public function actionCreate(){
$model = new NewsForm();
$model->create_at = (new DateTime())->format('Y-m-d H:i:s');
$model->update_at = (new DateTime())->format('Y-m-d H:i:s');
if ($news = $model->create()) {
//TODO Tosters
//var_dump($news->id);
return Yii::$app->getResponse()->redirect('/news/update/'. $news->id);
}
}
public function actionUpdate()
{
$request = Yii::$app->request;
$get = $request->get();
$model = News::findOne($get['id']);
if ($model->load(Yii::$app->request->post())) {
$model->create_at = (new DateTime())->format('Y-m-d H:i:s');
$model->update_at = (new DateTime())->format('Y-m-d H:i:s');
if ($model->status != News::STATUS_ARCHIVE) {
$model->publish_at = (new DateTime())->format('Y-m-d H:i:s');
}
if ($news = $model->create()) {
//TODO Tosters
Yii::$app->getResponse()->redirect('/news');
}
}
return $this->render('update', compact('model'));
}
Answer the question
In order to leave comments, you need to log in
Goodnight.
Why is there no such check in the create action?
This
can be successfully replaced by
Why such a design?
Isn't it easier to write like this?
if ($model->save()) {
// и получить id
$model->id;
The code is terrible, but it's most likely here:
You have it in both actions. Show the create method in the model.
By dates, I advise you to look at this:
demos.krajee.com/datecontrol - very convenient
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question