Answer the question
In order to leave comments, you need to log in
Yii2 how to get data from a table with relationships?
There are 2 tables (users and contacts) interconnected. Users (id, ...) - information about the user Contacts (id, my_id, contact_id) - who added whom to his contacts
In the users model, it is written:
public function getContact()
{
return $this->hasMany(Contacts::className(), ['contact_id' => 'id']);
}
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'contact_id']);
}
$qwe = Contacts::findOne(['my_id' => 1]);
$contacts = $qwe->getUser()->all();
$qwe = Contacts::findAll(['my_id' => 1]);
$contacts = $qwe->getUser()->all();
Answer the question
In order to leave comments, you need to log in
$qwe = Contacts::findAll(['my_id' => 1]);
$contacts = $qwe->getUser()->all();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question