Answer the question
In order to leave comments, you need to log in
How to check the existence of a user in yii2?
The user places an order. The data is received by the Order/create controller. You need to create an order in it by entering user_id.
In the user table, fields like user_id, user_name, and so on.
Now I did it like this:
if (!$user_data = $user->findByEmail($_POST['email'])) {
$attrs = [
'user_name'=>$_POST['name'],
...
];
$user->create($attrs);
$user_data = $user->findByEmail($_POST['email']);
}
$user_id = $user_data->user_id;
Answer the question
In order to leave comments, you need to log in
basically everything is correct.
notes:
1. not user_data, but user.
2. why the second findByEmail request if you already have a user in $user
3. use Yii::$app->getRequest()->getPost(..)
4. use PSR, in particular camelCase
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question