there are two dataobject classes "A" and "B" having one-to-many relationship. I want to get data containing columns from both parent and child dataobject. How to achieve this in Silverstripe 's ORM or SQL Query.
Example: Dataobject "A" has two events and first event contain one date info(start and end date). Second event contains two dates info (start and end date).
I want to query database to get information containing columns from both "A" and "B" and it should shows 3 row as result. one from "A" and two from "B".
I am using Silverstripe 2.4. A Movie Can have multiple dates to show. Movie dataobject has one-to-many relationship with MovieDate dataobject. I want to get all columns from Movie repeating each record of MovieDate associated with it. That means if movie has two dates, then I want to get two records
Title Desc StartDate EndDate
----- ---- --------- -------
Matrix AAA 2012-09-20 2012-09-20
Matrix AAA 2012-09-29 2012-09-29
Here is code for your kind consideration
<?php
class Movie extends DataObject
{
public static $db = array(
'Title' => 'Varchar',
'Desc' => 'Text',
);
public static $defaults = array(
'RedirectionType' => 'Internal',
);
public static $has_one = array(
'Image' => 'Image',
'Parent' => 'Page',
"LinkTo" => "SiteTree"
);
public static $has_many = array(
'MovieDates' => 'MovieDate'
);
static $summary_fields = array(
'Title' => 'Movie Title',
'Desc' => 'Movie Description'
);
function getRequirementsForPopup() {
Requirements::customCSS("
.iframe_wrap {
top: 35%;
}
");
}
public function getCMSFields()
{
$fields = new FieldSet(new TabSet('Root', $tab1 = new Tab('Main')));
$fields->addFieldsToTab('Root.Main', new TextField('Title', 'Movie Title'));
$fields->addFieldsToTab('Root.Main', new TextareaField('Desc', 'Movie Description'));
$tablefield = new DataObjectManager(
$this,
'MovieDates',
'MovieDate',
array('MovieStartDate' => 'Movie Start', 'MovieEndDate' => 'Movie End')
);
$tablefield->setPopupWidth(900);
$tablefield->setAddTitle("Movie Date/Time");
$fields->addFieldsToTab('Root.Main', $tablefield);
$fields->addFieldsToTab('Root.Main', new LiteralField("Space", "</br></br></br></br></br>") );
return $fields;
}
}
<?php
class MovieDate extends DataObject{
static $db = array(
'MovieStartDate' => 'Datetime',
'MovieEndDate' => 'Datetime',
);
static $has_one = array(
'Movie' => 'Movie'
);
function getCMSFields(){
$fields = new FieldSet();
$movieStartDate = new DateField('MovieStartDate', 'Movie Start');
$movieStartDate->setConfig('showcalendar', true);
$fields->push($movieStartDate);
$movieEndDate = new DateField('MovieEndDate', 'Movie End');
$movieEndDate->setConfig('showcalendar', true);
$fields->push($movieEndDate);
$space = new LiteralField("Space", "</br></br></br></br></br>");
$fields->push($space);
return $fields;
}
}