4
4
4ipS2016-03-13 03:48:51
Yii
4ipS, 2016-03-13 03:48:51

hasMany relation in YII2 how to use?

Hello .. Help me figure it out, otherwise I can’t.
there are two tables

CREATE TABLE `group_lists` (
  `id` int NOT NULL AUTO_INCREMENT,
  `id_player` int NOT NULL,
  `point` int(2) NOT NULL,
  `place` int(2) NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `match` (
  `id` int NOT NULL AUTO_INCREMENT,
  `id_player_in` int NOT NULL,
  `id_player_out` int NOT NULL,
  `goals_in` int(3) NOT NULL,
  `goals_out` int(3) NOT NULL,
  PRIMARY KEY (`id`)
);

The match.id_player_in and match.id_player_out fields have a connection to group_lists.id_player
When selecting, I can only get the results of matches or id_player_in or id_player_out
how to connect all this correctly so that one query and a cycle can be displayed?
Thank you. I am writing in Yii2
Current request
GroupLists::find()->joinWidth('matches')->groupBy('group_lists.id_player')->all();

There are also links that Gii generated
public function getMatches()
{
  return $this->hasMany(Match::class_name, ['id_player_in' => 'id_player']);
}

public function getMatches0()
{
  return $this->hasMany(Match::class_name, ['id_player_out' => 'id_player']);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-03-13
@webinar

GroupLists::find()->with(['Matches','Matches0'])->all();
more details here:
www.yiiframework.com/doc-2.0/guide-db-active-recor...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question