Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8539239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:19:01+00:00 2026-06-11T11:19:01+00:00

there are two dataobject classes A and B having one-to-many relationship. I want to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:19:02+00:00Added an answer on June 11, 2026 at 11:19 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are two intents on the receiver side which are called from the same
There are two table s : one is the master and one the detail
There are two tables Table 1 select * from manage_tcp; +----+----------+--------+ | id |
There are two unsorted lists containing Integers, need to find the common largest integer
I Have activity that get some data from the internet, and shows it to
I have two classes, one which inherits the other, for example:- public class A
Hi i have notice there two date times facebook gives, one is a date
Is there a quick way to get a XML sitemap into a hierarchical data
I have read a document that they say: In java there two types of
There are two lists: List<string> excluded = new List<string>() { .pdf, .jpg }; List<string>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.