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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Using HasManyComplexTableField to get a list of teams


Go to End


6 Posts   952 Views

Avatar
lozhowlett

Community Member, 151 Posts

21 October 2011 at 1:24am

Hi

I have a page type "Team" which consists of a team name. I also have an "Event" page type. Within the event page type I want to list multiple teams. So in a new tab in the event window I want to list all the teams I have in the database and me able to select (by a checkbox) multiple teams that are attending this event.

I think I could use HasManyComplexTableField? I also know I need a look up table between then event and the team... here is the code so far...

class Team extends Page {

	public static $db = array(
            'Name' => 'Text',
            'CarNumber' => 'Text',
            "Class" => "Enum('Petrol, Diesel', 'Diesel')",
            'ContactTelephone' => 'Text',
            'ContactEmail' => 'Text',
            'CarLocation' => 'Text'
	);

	static $has_many = array (
            'ImageResources' =>'ImageResource',
            'Drivers' => 'Driver'
	);
        
        public static $has_one = array(
            'ProfilePicture' => 'Image'
	);
        
        function getCMSFields() {
            $fields = parent::getCMSFields();
            $fields->addFieldToTab("Root.Content.Main", new TextField('Name'));
            $fields->addFieldToTab("Root.Content.Main", new TextField('CarNumber'));
            $fields->addFieldToTab("Root.Content.Main", new TextField('ContactTelephone'));
            $fields->addFieldToTab("Root.Content.Main", new TextField('ContactEmail'));
            $fields->addFieldToTab("Root.Content.Main", new TextField('CarLocation'));
            $fields->addFieldToTab("Root.Content.Main", new ImageField('ProfilePicture'));
            $fields->addFieldToTab("Root.Content.Main", new DropdownField('Class', 'Class', singleton('Team')->dbObject('Class')->enumValues()));
            $managerimages = new ImageDataObjectManager(
                    $this, // Controller
                    'Images', // Source name
                    'ImageResource', // Source class
                    'Attachment', // File name on DataObject
                    array(
                        'Title' => 'Title'
                    ), // Headings 
                    'getCMSFields_forPopup' // Detail fields
                    // Filter clause
                    // Sort clause
                    // Join clause
                );
                $fields->addFieldToTab("Root.Content.Image Gallery",$managerimages);

            return $fields;
        }

}
class Team_Controller extends Page_Controller {
	public static $allowed_actions = array (
	);

	public function init() {
            parent::init();
	}
}

<?php
class Event extends Page {

	public static $db = array(
            'Date' => 'Date',
            'Time' => 'Time',
            'isNextRace' => 'Boolean'
	);

        static $has_one = array (
            //'Track' => 'Track'
        );
        
	static $has_many = array (
            'ImageResources' =>'ImageResource',
            'Teams' => 'Team',
	);
        
        function getCMSFields() {
            $fields = parent::getCMSFields();
            $fields->addFieldToTab("Root.Content.Main", new DatePickerField('Date'));
            $fields->addFieldToTab("Root.Content.Main", new TimeField('Time'));
            $fields->addFieldToTab("Root.Content.Main", new CheckboxField('isNextRace'));
            $managerimages = new ImageDataObjectManager(
                    $this, // Controller
                    'Images', // Source name
                    'ImageResource', // Source class
                    'Attachment', // File name on DataObject
                    array(
                        'Title' => 'Title'
                    ), // Headings 
                    'getCMSFields_forPopup' // Detail fields
                    // Filter clause
                    // Sort clause
                    // Join clause
                );
                $fields->addFieldToTab("Root.Content.Image Gallery",$managerimages);

            return $fields;
        }

}
class Event_Controller extends Page_Controller {
	public static $allowed_actions = array (
	);

	public function init() {
            parent::init();
	}
}

Now the structure of the teams is simply put into a holding container and then adding each page under that...

- Team holder
-- Team 1
-- Team 2

I then have...

- Season
-- 2011
-----Event 1
-----Event 2

So that I can use teams across seasons...

In summary:

how do I get a list of teams into an event and then select which teams are attending that event?

Thanks for any help!

Avatar
MarcusDalgren

Community Member, 288 Posts

21 October 2011 at 1:49am

Well I'm guessing that each team can attend many events and each event can have many teams attending it so what you want is a many to many relationship.

To set that up you add:

public static $many_many = array(
"Events" => "Event"
);

to Team and then you can add:

public static $belongs_many_many = array(
"Teams" => "Team"
);

You then use the ManyManyComplexTableField to manage the relationship.

Avatar
lozhowlett

Community Member, 151 Posts

21 October 2011 at 2:06am

Hi, thanks for the reply. I have added that code... plus this...

$tablefield = new ManyManyComplexTableField(
                 $this,
                 'Teams',
                 'Team',
                 array(
                 'Name' => 'Name',
                 'CarNumber' => 'CarNumber'
                 ),
                 'getCMSFields_forPopup'
             );
            $fields->addFieldToTab("Root.Content.Team Manager",$tablefield);

i ran dev/build...

I now get a huge error list :)

[User Error] Couldn't run query: SELECT "SiteTree"."ClassName", "SiteTree"."Created", "SiteTree"."LastEdited", "SiteTree"."URLSegment", "SiteTree"."Title", "SiteTree"."MenuTitle", "SiteTree"."Content", "SiteTree"."MetaTitle", "SiteTree"."MetaDescription", "SiteTree"."MetaKeywords", "SiteTree"."ExtraMeta", "SiteTree"."ShowInMenus", "SiteTree"."ShowInSearch", "SiteTree"."HomepageForDomain", "SiteTree"."ProvideComments", "SiteTree"."Sort", "SiteTree"."HasBrokenFile", "SiteTree"."HasBrokenLink", "SiteTree"."Status", "SiteTree"."ReportClass", "SiteTree"."CanViewType", "SiteTree"."CanEditType", "SiteTree"."ToDo", "SiteTree"."Version", "SiteTree"."Priority", "SiteTree"."ParentID", "Team"."Name", "Team"."CarNumber", "Team"."Class", "Team"."ContactTelephone", "Team"."ContactEmail", "Team"."CarLocation", "Team"."ProfilePictureID", "SiteTree"."ID", CASE WHEN "SiteTree"."ClassName" IS NOT NULL THEN "SiteTree"."ClassName" ELSE 'SiteTree' END AS "RecordClassName", CASE WHEN "EventID" IS NULL THEN '0' ELSE '1' END AS "Checked" FROM "SiteTree" LEFT JOIN "Team" ON "Team"."ID" = "SiteTree"."ID" LEFT JOIN "Team_Teams" ON ("SiteTree"."ID" = "Team_Teams"."TeamID" AND "EventID" = '13') WHERE ("SiteTree"."ClassName" IN ('Team')) GROUP BY "SiteTree"."ClassName", "SiteTree"."Created", "SiteTree"."LastEdited", "SiteTree"."URLSegment", "SiteTree"."Title", "SiteTree"."MenuTitle", "SiteTree"."Content", "SiteTree"."MetaTitle", "SiteTree"."MetaDescription", "SiteTree"."MetaKeywords", "SiteTree"."ExtraMeta", "SiteTree"."ShowInMenus", "SiteTree"."ShowInSearch", "SiteTree"."HomepageForDomain", "SiteTree"."ProvideComments", "SiteTree"."Sort", "SiteTree"."HasBrokenFile", "SiteTree"."HasBrokenLink", "SiteTree"."Status", "SiteTree"."ReportClass", "SiteTree"."CanViewType", "SiteTree"."CanEditType", "SiteTree"."ToDo", "SiteTree"."Version", "SiteTree"."Priority", "SiteTree"."ParentID", "Team"."Name", "Team"."CarNumber", "Team"."Class", "Team"."ContactTelephone", "Team"."ContactEmail", "Team"."CarLocation", "Team"."ProfilePictureID", "SiteTree"."ID", CASE WHEN "SiteTree"."ClassName" IS NOT NULL THEN "SiteTree"."ClassName" ELSE 'SiteTree' END, "EventID" ORDER BY "Sort" LIMIT 10 OFFSET 0 Table 'funcupco_silverstripe.Team_Teams' doesn't exist

which i am guessing is to do with the code above not being set up right? Cheers

Avatar
MarcusDalgren

Community Member, 288 Posts

21 October 2011 at 2:27am

I'm sorry if I was unclear. The $belongs_many_many part goes in Event. However you need to think about from which end you want to manage the relationship. AFAIK you can only use the ManyManyComplexTableField in the class with the $many_many and not in the one with the $belongs_many_many.

Try commenting out the comment field and then run the dev/build so that the relationship gets added to the database. It might be running that code and since the relationship hasn't been added yet you get an error.

Avatar
lozhowlett

Community Member, 151 Posts

21 October 2011 at 2:48am

wow! Thats fantastic, thank you so much :) I switched around who belonged to whom and it works how I want...

Event many_many Teams
Teams belong to many events

Perfect!

Avatar
lozhowlett

Community Member, 151 Posts

21 October 2011 at 2:50am

Smurkas... email me your details if your interested in outsource work? lawrence@newedge.co.uk - not sure if that's allowed on this forum / admin - please delete if not appropriate. I couldn't find any posts saying dont! :)