X
X
XenK2017-05-05 13:38:44
Yii
XenK, 2017-05-05 13:38:44

Yii2 get ID of not yet created user?

There is a form for creating a user (full name, date of birth, phone number, photo).
Adding a photo was done only in update (because the user_id is known there):

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]);
echo $form->field($model_file, 'imageFile[]')->widget(
    FileInput::classname(), [
        'options'       => ['multiple' => true],
        'pluginOptions' => ['previewFileType' => 'any',
                            'uploadUrl'       => Url::to(
                                ['/users/upload?model_id=' . $model->id]
                            )],
    ]
);
ActiveForm::end();

The question is how to solve the problem with the definition of $model->id, in create?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-05-05
@webinar

Just load the photo in afterSave() where there is already an id

M
mitaichik, 2017-05-05
@mitaichik

Maxim Timofeev parvily said, but sometimes, in some thread of complex business logic, there really is a need to get the id of an entity that has not yet been saved. To solve this problem, you need to abandon auto-increments in tables and get the id from some thread of the counter, for example, redis incr.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question