Answer the question
In order to leave comments, you need to log in
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`)
);
GroupLists::find()->joinWidth('matches')->groupBy('group_lists.id_player')->all();
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
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 questionAsk a Question
731 491 924 answers to any question