Skip to content

2nd level includes don't show up when outputting/using the data #101

@vbakkenes

Description

@vbakkenes

I have the following data structure: Tournament -> Game -> Team. A Game has a double reference to Team (homeTeam & awayTeam).
When fetching this with the following code the homeTeam & awayTeam relations aren't populated correctly.

Am I missing something or is this a bug?

      return Tournament
        .where('slug', payload)
        .with('club')
        .with('games')
        .with('games.homeTeam')
        .with('games.awayTeam')
        .get()
        .then((res): any => {
          return res.getData()[0] || null;
        });

The result from the API
image

The result from console.log('game', game); (where the relations homeTeam & awayTeam are still null)
image

The Tournament class

import { ToOneRelation } from 'coloquent';
import { DateTime } from 'luxon';
import BaseModel  from './BaseModel';
import Club from './Club';
import Game from "./Game";

class Tournament extends BaseModel {
  static jsonApiType = 'tournaments';

  games() {
    return this.hasMany(Game);
  }

  getGames(): Game[] {
    return this.getRelation('games');
  }

  setGames(games: Game[]) {
    this.setRelation('games', games);
    return this;
  }


  public relationKeys = [
    'club',
  ];

  getSlug() {
    return this.getAttribute('slug');
  }

  getTournamentName() {
    return this.getAttribute('tournamentName');
  }

  getStartDate(): DateTime {
    const startDate = this.getAttribute('startDate');
    
    return DateTime.fromISO(startDate);
  }

  club(): ToOneRelation<Club, this> {
    return this.hasOne(Club);
  }

  getClub(): Club {
    return this.getRelation('club');
  }

  setClub(club: Club) {
    this.setRelation('club', club);

    return this;
  }

}

export default Tournament;

The Game class

import BaseModel from "./BaseModel";
import Team from "./Team";
import Tournament from "./Tournament";

class Game extends BaseModel {
  static jsonApiType = 'games';

  public getGameNumber() {
    return this.getAttribute('gameNumber');
  }

  tournament() {
    return this.hasOne(Tournament);
  }

  getTournament() {
    return this.getRelation('tournament');
  }

  setTournament(tournament: Tournament) {
    this.setRelation('tournament', tournament);

    return this;
  }

  homeTeam() {
    return this.hasOne(Team);
  }

  getHomeTeam() {
    return this.getRelation('homeTeam');
  }

  setHomeTeam(homeTeam: Team) {
    this.setRelation('homeTeam', homeTeam);

    return this;
  }

  awayTeam() {
    return this.hasOne(Team);
  }

  getAwayTeam() {
    return this.getRelation('awayTeam');
  }

  setAwayTeam(awayTeam: Team) {
    this.setRelation('awayTeam', awayTeam);

    return this;
  }
}

export default Game;

And the Team class

import BaseModel from "./BaseModel";
import Game from "./Game";

class Team extends BaseModel {
  static jsonApiType = 'teams';

  homeGames() {
    return this.hasMany(Game);
  }

  getHomeGames() {
    return this.getRelation('homeGames');
  }

  setHomeGames(homeGames: Game[]) {
    this.setRelation('homeGames', homeGames);

    return this;
  }

  awayGames() {
    return this.hasMany(Game);
  }

  getAwayGames() {
    return this.getRelation('awayGames');
  }

  setAwayGames(homeGames: Game[]) {
    this.setRelation('awayGames', homeGames);

    return this;
  }
}

export default Team;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions