17488 Posts in 4473 Topics by 1978 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1153 Views |
-
One to many relationship

15 June 2008 at 4:04pm
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 -
Re: One to many relationship

15 June 2008 at 4:09pm
Instead of the 2nd parameter being 'TourDatesFiltered' it should probably be 'TourDates', and then use the source filter directly in the table field instead of a separate function (TourDatesFiltered) to ->getComponents()
Sean
-
Re: One to many relationship

15 June 2008 at 4:10pm
Update: It actually DOES work if I select the checkbox beside the record, but there are two problems with this:
1. I don't want the user to have to do this. I want them to be able to enter the new TourDate and have it associated with that Tour when saved
2. The filter obviously isn't working, and I should have realized this when the TourDates were set to zero. All Tour Dates are available for all Tours, but if they are selected in Tour1, they are un-selectable in Tour2. I want them not to show at all.
Any help with the above two issues?
Cheers
Aaron -
Re: One to many relationship

15 June 2008 at 4:16pm
Hi Sean
"use the source filter directly in the table field instead of a separate function (TourDatesFiltered) to ->getComponents() "
I don't quite follow sorry. Use the source filter where in the table field?
Aaron -
Re: One to many relationship

15 June 2008 at 4:24pm
Sorry, what I meant is:
1. Change the 3rd argument to your HasManyComplexTableField from "TourDatesFiiltered" to "TourDates"
2. Add a 6th argument to your HasManyComplexTableField, which would be "TourID = {$this->ID}" (this is the $sourceFilter argument in __construct() for HasManyComplexTableField).Then you don't need the method TourDatesFiltered(), as you're filtering directly in the table. This may fix your filtering problem.
-
Re: One to many relationship

15 June 2008 at 4:52pm
Awesome, that solved the filter problem perfectly. Now only TourDates tied to a Tour are only visible on that Particular tour.
However the first problem is still there. When they were all showing before, you had to click the checkbox next to the item you added and Save before the TourDate was associated with that Tour. Otherwise the TourID (ParentID) would be zero.
So now, when you add a TourDate, it doesn't appear in the CMS. It's in the database table with a TourID of zero. Is it possible to auto insert the parent id when you save from the popup? -
Re: One to many relationship

15 June 2008 at 5:09pm
Found the solution to this here:http://silverstripe.com/site-builders-forum/flat/45030
$tourDateField->relationAutoSetting = true; after the Complex field declaration fixes the issue.
| 1153 Views | ||
|
Page:
1
|
Go to Top |


