I have found a few posts on this, but I can't make much sense of the particular problem I am having.
I have a page type called "Tour", and I want to associate departure dates with each Tour. These dates must only be accessible to the Tour they are entered for in the CMS. So a one to many relationship.
I have this displaying in the CMS OK. I can add dates. However these dates are accessible to every Tour page type. I have checked the database table "TourDates", and there is a field called "TourID", however, this is always set to zero, so I've missed something out here. Can anyone spot it?
Tour.php (condensed)
class Tour extends Page {
...
static $has_many = array(
'TourDates' => 'TourDate'
);
function TourDatesFiltered() {
return $this->getComponents("TourDates", "TourID=$this->ID");
}
function getCMSFields() {
$fields = parent::getCMSFields();
$tourDateField = new HasManyComplexTableField(
$this,
'TourDatesFiltered',
'TourDate',
array(
'StartDate' => 'Start Date',
'EndDate' => 'End Date',
'AvailableSeats' => 'Available Seats'
),
'getCMSFields_forPopup'
);
$tourDateField->setParentClass('Tour');
$fields->addFieldToTab( 'Root.Content.TourDates', $tourDateField );
}
....
And here is the TourDate Class:
TourDate.php (Full)
class TourDate extends DataObject {
static $db = array(
'StartDate' => 'Date',
'EndDate' => 'Date',
'AvailableSeats' => 'Int'
);
static $has_one = array(
'Tour' => 'Tour'
);
function getCMSFields_forPopup() {
$fields = new FieldSet();
$fields->push( new CalendarDateField( 'StartDate', 'Start Date' ) );
$fields->push( new CalendarDateField( 'EndDate', 'Start Date' ) );
$fields->push( new NumericField( 'AvailableSeats', 'Available Seats Left' ) );
return $fields;
}
}
No matter what, TourID in the TourDate is set to zero, meaning the filter function on Tour.php simply doesn't work. Any ideas?
Cheers
Aaron