Save Value twice

I am trying to safe a simple Value twice

I have my actionCreate:

i just wanted to safe the ID Attribut into the Nummer Attribut. So i added:

but nothing happens. I also tried to set it via Default Value.

public function rules()

{


   ['nummer', 'default', 'value' => $this->id],


}

but also get me ‘not set’ after save a new record.

can you please help me what i am doing false?

Is ‘id’ attribute of the model the primary key? If so, it is not set until it is saved (i.e., inserted into the table with a new primary key). So the “default” rule does not work as you expect.

In your current code of “actionCreate”, you set $model->nummer with the value of $model->id. It’s OK. But the model is not saved after $model->nummer has been set. You have to save the model twice:




public function actionCreate()

{

    $model = new Kunde();


    if ($model->load(Yii::$app->request->post()) && $model->save()) {

        $model->nummer=$model->id;

        $model->save(false); // this is it!

        return $this->redirect(['view', 'id' => $model->id]);

    } else {

        return $this->render('create', [

            'model' => $model,

        ]);

    }

}



But, well, do you really need ‘nummer’ attribute? Why don’t you directly access ‘id’?

thank you!!!!

Allright i understund and yeah i dont need ‘nummer’ really, it was more a challenge to myself.

I am new in the business and i wanted to know why i cant manage this simple question.

But you are absolutly right about not storing Values redundant.

-> Sorry for my englisch :D