Hello. I am adding friends.

Here is the table:

enter image description here

By default, status = 0. As the user confirmed the second, then 1. user_id is the one who first added. fried_id someone added. Tell me how I can now correctly select data from the table, namely: friends of a particular user.

public function actionIndex($id){ $user = User::findOne($id); if(empty($user)){ throw new \yii\web\HttpException(404, 'Такого пользователя нет!'); } $list_friend = Friend::find()->where(['status' => 1]) ->andWhere(['friend_id' => $id]) ->andWhere(['user_id' => $id]) ->limit(5) ->orderBy('RAND()')->all(); // так не правильно... } 

user_id - submitted a friend request. The friend_id is the one who wanted to add. From the table it is clear that the user with id 3 has added a user with id 1. etc.

    1 answer 1

    Good evening.

     $list_friend = Friend::find()->where(['status' => 1]) ->andWhere(['friend_id' => $id]) ->orWhere(['user_id' => $id]) ->limit(5) ->orderBy('RAND()')->all(); 

    ps

    Tell me how I can now correctly select data from the table, namely: friends of a particular user.

    user_id is the one whose friends you want to get? Then why do you need friend_id at all?

    pss One user can have several friends, so hasOne () should be replaced with hasMany ();

    It was also necessary to indicate the presence of links and describe the problem completely.

    All the details can be found on the link.

    psss $ list_friend-> friend-> username;

    • so you choose only by user_id. Not right - artem55555p
    • supplemented the answer. - user216615
    • user_id - submitted a friend request. friend_id - the one you wanted to add - artem55555p
    • In this case, add your question. From it it is not clear what exactly you need. - user216615
    • added addition - artem55555p