M
M
Mikhail Shatilov2014-09-23 18:29:15
Yii
Mikhail Shatilov, 2014-09-23 18:29:15

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;

Tell me how to do it right.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Zelenin, 2014-09-23
@iproger

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 question

Ask a Question

731 491 924 answers to any question