Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vbakkenes opened this issue Jun 2, 2022 · 0 comments
Open

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

vbakkenes opened this issue Jun 2, 2022 · 0 comments

Comments

@vbakkenes
Copy link

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant