21278 Posts in 5728 Topics by 2599 members

# General Questions

SilverStripe Forums » General Questions » Two issues with ComplexTableFields in a popup [Solved]

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

Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w

 Page: 1 Go to End Author Topic: 1650 Views

Community Member
145 Posts

#### Two issues with ComplexTableFields in a popup [Solved]

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

• Hamish

Community Member
712 Posts

#### Re: Two issues with ComplexTableFields in a popup [Solved]

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; } • AdamJ Community Member 145 Posts #### Re: Two issues with ComplexTableFields in a popup [Solved] 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.

Community Member
145 Posts

#### Re: Two issues with ComplexTableFields in a popup [Solved]

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.

Community Member
145 Posts

#### Re: Two issues with ComplexTableFields in a popup [Solved]

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?

Community Member
145 Posts

#### Re: Two issues with ComplexTableFields in a popup [Solved]

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.

 1650 Views Page: 1 Go to Top

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