Answer the question
In order to leave comments, you need to log in
Yii2 / hasOne / hasMany or what am I doing wrong?
We have 3 tables:
Teams (id, capitan_id, ...) - list of teams
Fighters (id, ...) - list of fighters Rosters (id,
fighter_id
, team_id) - table of many-to-many
relationships
on the view of a specific team, display a list of its fighters, as well as its captain, I understand
But I don’t understand how to display a list of teams and in each captain and a list of fighters.
Model:
<?php
namespace app\models;
use Yii;
use app\models\Fighters;
class Clans extends \yii\db\ActiveRecord {
public static function tableName() {
return 'teams';
}
public static function allClans() {
$data = self::find()->all();
return $data;
}
public function getCapitans() {
//получаю капитана команды
return $this->hasOne(Fighters::className(), ['id' => 'capitan_id']);
}
public function getMembers() {
//получаю бойцов команды
return $this->hasMany(Fighters::className(), ['id' => 'fighter_id'])
->viaTable(Rosters::tableName(), ['team_id' => 'id']);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question