S
S
Sergey Evstigneev2015-12-03 16:10:15
Yii
Sergey Evstigneev, 2015-12-03 16:10:15

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']);
  }
    
}

Tell me please! Stupor right!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shamyyl, 2015-12-03
@sergevstigneev

You don't have to correctly display a many-to-many relationship.
Yii2. Linking through an intermediate table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question