Jump to:

3377 Posts in 1001 Topics by 714 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Players, teams and fixtures SS2.4.7

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 376 Views
  • lozhowlett
    Avatar
    Community Member
    140 Posts

    Players, teams and fixtures SS2.4.7 Link to this post

    Hi Everyone

    I am building a site for a rugby club in the UK.

    A fixture has 2 teams, home and away, it also has many players (PlayerResult)

    PlayerResult records who is playing at that fixture and how they did, i.e. player 1 score 2 goals.

    A player has one team

    In the admin system, I want my users to be able to add a new fixture, then in a tab Called "Home Team", which displays all the players for that team, tick which players are playing then be able to add how many tries and how many goals they scores for that fixture. Then repeat this for the away team.

    I am finding it difficult to work out the correct way to do this, i need help with how to work out the CMS fields for fixtures and to make sure it populates with the correct team data for each based on HomeTeam and AwayTeam IDs. Below is some sample code, comments welcome!

    class Fixture extends Page
    public static $has_one = array(
    'HomeTeam' => 'Team',
    'AwayTeam' => 'Team'
       );

    static $has_many = array (
    'EventImageResources' =>'EventImageResource',
    'FixturePlayers' => 'FixturePlayer'
       );

    class TeamPlayer extends DataObject {

       public static $db = array(
    'PlayerName' => 'Text',
    'PlayerPosition' => 'Text'
       );

       public static $has_one = array(
    'Team' => 'Team'
       );

    public static $has_many = array(
    'PlayerResults' => 'PlayerResult'
    );

    }

    class PlayerResult extends DataObject {

    public static $db = array(
    'Tries' => 'Int',
    'Goals' => 'Int'
    );

    public static $has_one = array(
    'TeamPlayer' => 'TeamPlayer',
    'Fixture' => 'Fixture'
    );

    function getCMSFields_forPopup() {
    $fields = new FieldSet();

    $oData = (DataObject::get('TeamPlayer','','',SOME LEFT JOIN NEEDS TO BE HERE,''));
    if ($oData) {
    $CategoriesSource = $oData->toDropDownMap('ID','PlayerName');
    } else {
    // no categories there yet, might put a literalfield to tell the user
    }
    $dropdown = new DropdownField('TeamPlayerID', 'TeamPlayer', $CategoriesSource, $this->TeamPlayerID);
    $fields->push($dropdown);

    $fields->push(new NumericField('Tries'));
    $fields->push(new NumericField('Goals'));
    return $fields;
    }

    }

    class Team extends DataObject {

       public static $db = array(
    'TeamName' => 'Text'
       );

       public static $has_one = array(
    'TeamLogo' => 'Image',
    'LeagueTableResult' => 'LeagueTableResult'
       );

    public static $has_many = array(
    'TeamPlayers' => 'TeamPlayer'
    );

    }

    Thanks very much in advance for any help!

  • lozhowlett
    Avatar
    Community Member
    140 Posts

    Re: Players, teams and fixtures SS2.4.7 Link to this post

    Found the solution....

    $tablefield = new ManyManyComplexTableField(
    $this,
    'TeamPlayers',
    'TeamPlayer',
    array(
    'PlayerName' => 'Name',
    'PlayerPosition' => 'Position'
    ),
    'getCMSFields_forPopup',
    'Team.ID='.$this->HomeTeamID,'TeamPlayer.PlayerName','LEFT JOIN Team ON TeamPlayer.TeamID=Team.ID'

    );

  • lozhowlett
    Avatar
    Community Member
    140 Posts

    Re: Players, teams and fixtures SS2.4.7 Link to this post

    Ok that worked perfect for the home team, however when I replicated it for away team it now takes both of them out and displays blank...

    $fields->addFieldToTab("Root.Content.HomeTeam",$tablefield);

    $tablefield = new ManyManyComplexTableField(
    $this,
    'TeamPlayers',
    'TeamPlayer',
    array(
    'PlayerName' => 'Name',
    'PlayerPosition' => 'Position'
    ),
    'getCMSFields_forPopup',
    'Team.ID='.$this->AwayTeamID,'TeamPlayer.PlayerName','LEFT JOIN Team ON TeamPlayer.TeamID=Team.ID'

    );
    $fields->addFieldToTab("Root.Content.AwayTeam",$tablefield);

    Any ideas?

    376 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.