Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Players, teams and fixtures SS2.4.7


Go to End


3 Posts   1097 Views

Avatar
lozhowlett

Community Member, 151 Posts

27 July 2012 at 10:09pm

Edited: 27/07/2012 10:09pm

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!

Avatar
lozhowlett

Community Member, 151 Posts

28 July 2012 at 1:42am

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'
                    
             );

Avatar
lozhowlett

Community Member, 151 Posts

28 July 2012 at 1:56am

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?