[Silverstripe 2.4]: How to get records containing

2019-06-09 08:35发布

问题:

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;
}

}

回答1:

what you're after is a 'left join', i think. unfortunately, silverstripe has no means of retrieving data on a joined table using the built-in ORM queries (see http://doc.silverstripe.org/framework/en/2.4/topics/datamodel#joining)

so you need to go with a plain sql query here:

    $query = "
        SELECT `Movie`.*, `MovieDate`.*
        FROM `Movie`
        LEFT JOIN `MovieDate` ON `Movie`.`ID` = `MovieDate`.`MovieID`
        ORDER BY `Movie`.`Title`
    ";
    $records = DB::query($query);

you may loop over $records now using foreach:

    foreach($records as $record) {
        print_r($record);
    }

also see this forum thread for additional information: http://www.silverstripe.org/general-questions/show/12745



回答2:

You can create DataObjectSet that contains the result of separate DataObject::get() calls.

$a = DataObject::get(...);
$b = DataObject::get(...);

$all = new DataObjectSet();

$all->merge($a);
$all->merge($b);

$all->sort('date','desc');

The code is untested, but hopefully you get the idea. Reference the DataObjectSet docs to see the full API.

Edit: This assumes you are using SilverStripe 2.x - see the updated 3.0 datamodel docs for usage with SS 3.0, most notably the DataObject::get() calls.