How do I output the data of the interlocutor?

Good afternoon.
I have a table messages with the messages:
id
message - the message
from_whom - who
whom - to whom

How to lead those with whom correspondence was carried on?

I tried to use
$this->hasOne(Users::className(), ['user_id' => 'from_whom']);

tripped up on everyone and the where and orWhere and not user_id = my id does not work
July 2nd 19 at 17:28
2 answers
July 2nd 19 at 17:30
Solution
Good afternoon.
As Maxim wrote, You need to link by id(primary key) of the user and if You are not in the Users table field user_id, then how are You going to connect?While in the field from_whom You, I hope the user id is recorded and the field type matches the field type in the users table?
the primary key is user_id. why it all took that it is not?! in the field from_whom(from whom) and
whom(who) recorded user IDs. you should print the data of the interlocutor, but the problem is that from_whom and whom can be my id - aliya_Green commented on July 2nd 19 at 17:33
: I mean, Your id?
In what form is data stored in from_whom and to whom?
Why do You in the users table user_id? - Dion77 commented on July 2nd 19 at 17:36
: If id is a primary key on this field and communicate - Dion77 commented on July 2nd 19 at 17:39
I wrote a message, my id and any other user who wrote the message there is written: in from_whom - who wrote, in whom -to whom he wrote
I have a primary key user_id. why all precipices to this id?
need to somehow bring the person with whom the user has corresponded. - aliya_Green commented on July 2nd 19 at 17:42
Why You in the users table id and user_id? Just one id - Dion77 commented on July 2nd 19 at 17:45
: there is no id. in the users has a primary key user_id.
id is only in message, but it is nothing to do with it - aliya_Green commented on July 2nd 19 at 17:48
In "messages" table from_whom and whom should be intenger()->notNull
And then the link in the "messages" table as You described
public function getWhomUser(){
return $this->hasOne(Users::className(), ['id' => 'whom_id');
}
And
public function getFromWhomUser(){
return $this->hasOne(Users::className(), ['id' => 'from_whom');
} - Dion77 commented on July 2nd 19 at 17:51
: It is logical in the users table to have a simple id - Dion77 commented on July 2nd 19 at 17:54
: thanks! It has suggested that is necessary) - aliya_Green commented on July 2nd 19 at 17:57
July 2nd 19 at 17:32
Based on standard practices, I think we need not user_id and id just
how the name change can help? - aliya_Green commented on July 2nd 19 at 17:35
: what's the change. Typically, a user model has no field user_id, and there is a field id - Dion77 commented on July 2nd 19 at 17:38
: this is since when the model user cannot change the field names? :) - Dion77 commented on July 2nd 19 at 17:41
: the relationship which you described should work if you have a model Users and it has a user_id field that corresponds to the field from_whom the second model. - aliya_Green commented on July 2nd 19 at 17:44
: I didn't say impossible. I say that it is possible to model Users there is no user_id field. The standard model has a field id, not user_id. - Dion77 commented on July 2nd 19 at 17:47
such a model field is. the problem is that from_whom(from whom) - it could be my id or another user, and then whom (who) are also. and output only the data of another user, the interlocutor. - aliya_Green commented on July 2nd 19 at 17:50

Find more questions by tags PHPYii