1

I have an extended question to this question.

What if the player belong to more than one team?

I have this

json

  "Players" : {
    "-YRHd4IjrjsBXx__B" : {
      "name" : "The best forward",
      "creationDate" : "2016-02-26 15:50:39",
      "teams" : {
         "-KAByPeIz4IjrjsBXx__B" : true,
         "-KEFPuCXcqOah_GJwsMCu" : true,
         "-KEwuQxvGpYTEJ7YQ58-l" : true,
         "-KKF8vPtf8J7cfqFh2PLm" : true
      },
    },
  etc...
  }

players-service.js

getPlayers: function(teamid) {
  var Players = {};
  var teamsIndex = ref.child('teams/' + teamid + '/players/');
  var playersIndex = ref.child('players/');
  teamsIndex.on('child_added', function(snapshot) {
    var playerKey = snapshot.key;
    playersIndex.child(playerKey).on('value', function(playersnap){
      $timeout(function() {
          console.log("key", playerKey);
          players[playerKey] = playersnap.val();
      });
    });
  });
  teamIndex.on('child_removed', function(snapshot) {
    $timeout(function(snapshot) {
      delete players[snapshot.key()];
    });
  });

  return players;
}

But it returns a list of object. I know that I could probably query/change the data structure to/in firebase and return it as a $firebaseArray which I prefer as I use angularfire.

2
  • Do you mean that you want getPlayer to return an array instead of an key value object? Commented Jun 19, 2016 at 19:12
  • I'm confused. I'm stuck in between the nosql and SQL worlds. I think i wonder if my solution are the way to go. It could be that need to stick to this solution or denormalize my data more. Haven't diceded yet. A the moment I'm tring to denormalize more. aka put teamid: true in the root of the player node. "Players" : { "-YRHd4IjrjsBXx__B" : { "name" : "The best forward", "creationDate" : "2016-02-26 15:50:39", "-KAByPeIz4IjrjsBXx__B" : true, "-KEFPuCXcqOah_GJwsMCu" : true, } } Commented Jun 19, 2016 at 19:49

1 Answer 1

2

You usually structure your data depending on how you want to retrieve them.

From my understanding (correct me if I'm wrong) you want to get all the players in a team. For this purpose I would use this structure:

"Players": {
  "player1": {...},
  "player2": {...},
  "player3": {...}
},
"Teams': {
  "team1": {...},
  "team2": {...}
},
"TeamPlayers" : {
  "team1": {
    "player1": true,
    "player2": true
  },
  "team2": {
    "player1": true,
    "player3": true
  }
}

Or using an array

"TeamPlayers" : {
  "team1": [
    0: "player1",
    1: "player2"
  ]
}
Sign up to request clarification or add additional context in comments.

5 Comments

I love you! seriously I do!
Ahah happy to help! Thanks for upvoting. I've edited with another solution
This phrase made it to me: "You usually structure your data depending on how you want to retrieve them."
how can I show a list of all player1's teams. do I have to do many queries?
In this case I would use somethings likeref("TeamPlayers").orderByChild("player1").startAt(true). I'm not sure this is the right code but it is the correct approach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.