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

Two issues with ComplexTableFields in a popup [Solved]


Go to End


6 Posts   2483 Views

Avatar
AdamJ

Community Member, 145 Posts

26 May 2009 at 12:51am

Edited: 05/06/2009 3:21am

I have a nested relationship as follows:

Many Workshops.
Each Workshop has many Units.
Each Unit has one Location and many Instructors.
(Locations and Instructors belong to many Units)

I solved having nested associations by extending location and instructors from page, and putting them in their own holder in the site tree. Units are extended from DataObject, and are included in a tab on the workshops page with a HasManyComplexTableField.

In the popup for Units, I have included two ComplexTableFields. A HasMany for Instructors, and a HasOne for the Location. When I edit one of the Units I've already created (before I added the two ComplexTableFields) it loads fine, but if I try to add a new Unit, it gives me the following error.

Fatal error: Call to a member function Instructors() on a non-object in C:\wamp\www\delta\sapphire\forms\HasManyComplexTableField.php on line 91

That line is the $selectedItems line in the following:

function selectedItemIDs() {
		$fieldName = $this->name;
		$selectedItems = $this->form->getRecord()->$fieldName();
		$itemIDs = array();
		foreach($selectedItems as $item) $itemIDs[] = $item->ID;
		return $itemIDs;
	}

If I comment out the instructors ComplexTableField, the form displays properly, but won't save changes to the location ComplexTableField, which leads me to my second issue.

My second issue is that when I try to edit one of the units I've already added, alter the two ComplexTableFields and press save, it won't retain my changes to either of the two ComplexTableFields.

I've put the contents of unit.php, location.php and instructor.php in a pastie here

Avatar
Hamish

Community Member, 712 Posts

27 May 2009 at 12:49pm

Try testing for the existance of $selectedItems first.

function selectedItemIDs() {
	$fieldName = $this->name;
	$selectedItems = $this->form->getRecord()->$fieldName();
	$itemIDs = array();
	if($selectedItems)
		foreach($selectedItems as $item) $itemIDs[] = $item->ID;
	return $itemIDs;
}

Avatar
AdamJ

Community Member, 145 Posts

27 May 2009 at 1:01pm

Switched that code out for the one in HasManyComplexTableField.php but still errored on this line:

$selectedItems = $this->form->getRecord()->$fieldName();

I should also note, I am using 2.3.2-rc1.

Avatar
AdamJ

Community Member, 145 Posts

29 May 2009 at 9:15pm

I upgraded to 2.3.2 rc2 today and the "Call to a member function Instructors()" error disappeared.

What still persists though is the fact that it won't save those two relationships tables. Another bug has appeared also. I use the 'title' field as part of the table columns, the default table field that comes when extending from page. Instead of showing the title, its showing the id of the object.

Avatar
AdamJ

Community Member, 145 Posts

2 June 2009 at 6:48pm

Sorry to multiple post here, but my issue is still going on. I've tried to get some advice from the IRC channel a couple times, and people have tried to help, but havent been able to beat it yet.

As in my last post, my issue now is that the ComplexTableFields just won't save. They show a table of Locations and Instructors, but they just won't save.

My current thinking is that I restructure my code to be the following to try and get around the non-saving issue.

Workshops extends Page.
Child: Units extends Page.
Tabs within Units:
Location extends DataObject.
Instructors extends DataObject.

Lets say using that structure I have the following data:

Workshop A
- Unit 1
- - Location A
- - Instructor A
- - Instructor C
- Unit 2
- - Location B
- - Instructor B
- - Instructor C

Workshop B
- Unit 1
- - Location A
- - Instructor A
- - Instructor D

How would I get a list of all the unique instructors (A,B,C in that example) for Workshop A? Similarly, how would I get a list of all the workshops that an Instructor (eg: Instructor A - Workshop A & B) is a part of?

Avatar
AdamJ

Community Member, 145 Posts

5 June 2009 at 3:20am

Alright so my last bump for this thread. I just wanted to let people know I fixed my issue thanks to some help in the irc chat from ajshort and simon_w. I completely got rid of the complextablefields from my popup fields, and replaced the ManyMany field with a CheckboxSetField and the HasOne with a TypeDropdown. I've included the code below for other people. It also shows pulling in the list of instructors and putting it straight into the checkbox fields using map(). I will also probably manage the two datasets of Instructors and Location from a ModelAdmin screen just for some fun.

$sqlQuery = new SQLQuery(
        array('Instructor.ID AS ID', 'Sitetree.Title AS Title'),
        'Instructor LEFT JOIN Sitetree ON Instructor.ID = Sitetree.ID'
      );
      $map = $sqlQuery->execute()->map();
      
      $fields->push( new CheckboxSetField("Instructors","Instructors",$map) );
   
      $fields->push( new TypeDropdown("MyLocationID", "Location", "Location") );

If i can improve what I have done in anyway, I'm all ears.