I think I was on the right track, and I reckon I have a pretty good solution. I hope someone can have a look at this and give me some pointers.
I created a third class called CourseDate that just contains a date. The reason I did this was so that I could use it in my $has_many array, which needs to have the $has_one relation in the other direction.
Instead of using a ComplexFieldTable on the second one-to-many relation, I used a TableField.
Here's my code:
<?php
//file mysite/code/CoursePage.php
class CoursePage extends Page {
static $db = array();
static $has_one = array();
static $has_many = array(
'Courses' => 'Course'
);
//list the location and fee entries in a table
function getCMSFields() {
$fields = parent::getCMSFields();
$tablefield = new ComplexTableField(
$this,
'Courses',
'Course',
array(
'Location' => 'Location',
'Fee' => 'Fee'
),
'getCMSFields_forPopup'
);
$tablefield->setAddTitle( 'A Course' );
$fields->addFieldToTab( 'Root.Content.Courses', $tablefield );
return $fields;
}
}
class CoursePage_Controller extends Page_Controller {
}
?>
Next I have a 'Course' which is made up of multiple fee/location pairings. The reason for this is that courses in different locations may have different costs. I extend DataObject because I don't need all the Page stuff.
<?php
class Course extends DataObject {
public static $db = array(
'Location' => 'Text',
'Fee' => 'Currency'
);
public static $has_one = array(
'CoursePage' => 'CoursePage'
);
public static $has_many = array(
'CourseDates' => 'CourseDate'
);
//when an entry in the 'Courses' tab is clicked, I want to show a table of dates
function getCMSFields_forPopup() {
$fields = new FieldSet();
$fields->push( new TextField( 'Location' ) );
$fields->push( new CurrencyField( 'Fee' ) );
$ctf = new TableField(
'CourseDates',
'CourseDate',
array( 'Date' => 'Date' ),
array( 'Date' => 'DateField' )
//array( 'Date' => 'CalendarDateField' ) //the bottom of this gets cut off obscuring the dates!
);
$fields->push( $ctf );
return $fields;
}
} //end class
Now this bit I'm not 100% on but it works:
<?php
class CourseDate extends DataObject {
//it's just a 'Date' named 'Date'
public static $db = array(
'Date' => 'Date'
);
//make the link back to the 'parent'
public static $has_one = array(
'Course' => 'Course'
);
} //end class
Now this all seems pretty good except that when I use CalendarDateField in my Course object the bottom of the calendar gets cut off.
My second choice would be the composite date field, but I can't get that to load the date correctly. So I fall back on DateField.