I have a best practice question as I’m still struggling with the whole concept of Yii (2). And I’m a network engineer and not much of a SE
I would basically like to access my model after a POST and don’t know where to initialize it to be able to do so.
Say I have a controller with one actionMethod():
public function actionProvision()
{
$model = new Model();
if ($model ->load(Yii::$app->request->post()) && $model->validate()){
//here I'd like to access my Model
return $this->render('provision-confirm', ['model' => $model]);
} else {
$model ->loadStuffFromSSH();
$model ->loadALotMore();
return $this->render('provision', ['model' => $model]);
}
}
In the Model I have the functions loadStuffFromSSH(), etc. these functions load various stuff which are then held in public accessible vars/arrays in the Model.
The provision-View consists basically of a Form with a few drop-down lists on which the user can select various things and then hit the "provision" button.
Now I would like to access my model because I need quite a few things which are not getting submitted in the HTTP-POST.
How would you ideally accomplish this?
Or is it better to save all those things to a temporary DB-Table or submit EVERYTHING in hidden fields?
Thank you for pointing me here in the right direction
What is the problem in accessing model in ‘//here I’d like to access my Model’ comment row ?
Why should you save to a temporary DB-Table or submit EVERYTHING in hidden fields?
Take care that you enter in this block
if ($model ->load(Yii::$app->request->post()) && $model->validate()){
only if $_POST[$model->formName()] is set and data are validated. So it’s not enough that user submits form, but data have to verify all model rules validation.
Thank you for your answer but I do not understand…
the function actionProvision() is called again when I submit the Form.
So I have again a new $model which is then filled with (only) the data POSTed in the Form by calling
$model->load(Yii:$app->request->post())
but my previous instance of $model is lost.
But I like to access data contained in the $model before the POST.
Maybe the question is really: where should I put data that is gathered when calling actionProvision() initially and still be accessible when calling actionProvision() again (because the Form has been submitted).