0

I'm using a Collection class to store objects of 'players'. Now in my Bet class I want to update the points of one of the players.. But I think I'm only updating it in once instance of my Collection. I'm not quite sure how to talk to the global collection object. Here's my code

Index.php I'm creating a new PlayerCollection and passing it to my Parser class

$app->get('/', function () use ($twig) {

    try {
        $players = new PlayerCollection();
        $matchParser = new MatchParser($players);
        $matches = new MatchCollection($matchParser->parse('../data/data.json'));
    } catch(JsonException $e) {
        die($e->getMessage());
    }

    echo $twig->render('home.html', [
        'matches' => $matches->all(),
        'players' => $players
    ]);

});

Parser Class I'm accepting the PlayerCollection in my constructor and setting the Player object in the Bet class using the following code.

The this->players->find() functionality is tested and returns the correct object from the collection

$bet = new Bet();
$bet->setPlayer($this->players->find($name));

Bet Class

class Bet {

    private $player;
    private $teamHome;
    private $teamAway;
    private $scoreHome;
    private $scoreAway;


    public function getPoints($scoreHome, $scoreAway)
    {
        if($scoreHome == $scoreAway ) {
            $this->player->setPoints(100);
        }
    }
}

I call the function to calculate the score inside my Twig template

{{ bet.getPoints(match.getHomeScore(), match.getAwayScore()) }}

When I echo out the score of the player inside my bet class it's working.. But my PlayerCollection hasn't updated on my index.php page.

It might be my Template? The value's I want to update are already placed by twig, only later in the template I call the method to update the variables previous, but already placed

Any idea what I might be doing wrong? Thank you!

Community
  • 1
  • 1
Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125
  • You are only adding points to the player inside the Bet class. That has no effect on the actual player object inside the Parser object – RiggsFolly Jun 07 '16 at 15:32
  • Should I update the original Collection with the new player after the points increase? – Miguel Stevens Jun 07 '16 at 15:34
  • 1
    This is missing something. I tried this http://codepad.org/TpprZ5ZE and it seems to work as expected. In theory anyway. – NotJustin Jun 07 '16 at 16:26
  • Thank you for your time, Maybe the index overwrites the newer created one, I'll add the code – Miguel Stevens Jun 07 '16 at 17:10
  • I updated the index.php code, You can see I pass the $players to my twig view.. Maybe this is not the right behaviour from my part? – Miguel Stevens Jun 07 '16 at 17:14
  • Thanks for your time, isn't & deprecated since php5.4? I'm trying without the find() method now. – Miguel Stevens Jun 07 '16 at 17:32
  • It's my own collection class, I'll add it to my code., as well: It might be my Template? The value's I want to update are already placed by twig, only later in the template I call the method to update the variables previous, but already placed – Miguel Stevens Jun 07 '16 at 17:34
  • Not too familiar with twig but maybe this helps: http://twig.sensiolabs.org/doc/functions/attribute.html – NotJustin Jun 08 '16 at 10:23

1 Answers1

-1

when you created your new Bet() object, you copied an existing instance of one of your player objects into your Bet object, thus not altering the original one.

code that was previously here didn't make sense, oops

Yinkaking
  • 64
  • 8