0

Just started with Yii (two weeks ago).

TL;DR

Creating through Gii generated views not working, but update does, even they share the form and controller not edited.

Problem:

I use the Yii2 advanced app template.

Then I generated a model through the model generator and controller/views through the CRUD generator.

The only thing I changed is removing the two datetime fields "created_at" and "created_by" from the form and added a TimestampBehavior:

public function behaviors()
{
    return [
        [
            'class' => TimestampBehavior::className(),
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
              ],
              // if you're using datetime instead of UNIX timestamp:
              'value' => new Expression('NOW()'),
        ],
    ];
}

The strange case I have is, that the update is working, but the create not. It shows no error, it just stay on the page or renders it newly?

This is the untouched code from the controller:

(Am I getting it right that the "$model->save" in the if should save it to the databse?)

CREATE:

public function actionCreate()
{
    $model = new Seminar();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

UPDATE:

public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

If further informations are needed please comment. Thanks.


UPDATE - Solution

After the two helpful answers I tried dump the error and it shows

array(1) { ["created_at"]=> array(1) { [0]=> string(32) "Created At darf nicht leer sein." } }

As suggested I left the "created_at" in the "required" section of the rules. After cleaning it out the create works. I think it did not show me the error because I deleted the field in the form, since the user should not enter data dirctly.

mfgmicha
  • 3,784
  • 1
  • 25
  • 26
  • I didn't quite understood when you said **"It shows no error, it just stay on the page or renders it newly"**... You mean the url changes to action create, the pages loads again but the content is the same of action update? Or the page doesn't even load at all? – Clyff Dec 02 '15 at 15:51
  • Is it possible to show us the views? – Clyff Dec 02 '15 at 15:51
  • The views are the standard views generated by gii. And yes, for me it seems like the page loads again, the content of the form is prefilled with my entered data, bit no error or something like that. – mfgmicha Dec 02 '15 at 15:55
  • To be clear, I am on the /create action, want to save the form data by clicking the "create"-Button, the page reloads (same action in url - create) and just showing the form again with my data instead of the view action with the newly created id. – mfgmicha Dec 02 '15 at 16:01

2 Answers2

1

The Controllers you showed are ok.

There must be something wrong with the model. You can check what the problem is by editing the actionCreate:

public function actionCreate()
{
    $model = new Seminar();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        var_dump($model->getErrors());
        /*return $this->render('create', [
            'model' => $model,
        ]);*/
    }
}

Now, about your changes: since you are using the column names created_at and updated_at you can simplify the behavior by setting:

public function behaviors()
{
    return [
        TimestampBehavior::className()
    ];
};

Also, check your model rules() if the fields created_at or updated_at are there, and remove it. They are not needed anymore.

Clyff
  • 4,046
  • 2
  • 17
  • 32
1

Check the rules in your model and try again. Also, try with $model->save(false). It will save the form without check validation, so the problem is that data doesn't validate.