Jump to:

3433 Posts in 1058 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Retrieving a list of distinct entries from a DataObject

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2066 Views
  • nekranox
    Avatar
    Community Member
    31 Posts

    Retrieving a list of distinct entries from a DataObject Link to this post

    Hey guys,

    I have a DataObject called "Job" here's what it looks like:

       static $db = array(
          'Title' => 'Varchar(24)',
          'Description' => 'Text',
          'HoursPerWeek' => 'Text',
          'Location' => 'Varchar(24)',
          'RequiredRoutewayCourse' => 'Text',
          'HourlyWageOrSalary' => 'Currency',
          'ExpiryDate' => 'Date',
       );

    In "JobCategory.php" which is a page that lists all of my jobs, I would like to retrieve a list of all unique "RequiredRoutewayCourse" entries from the Job table in the database.

    What is the best way to do this?

    Thanks!

  • nekranox
    Avatar
    Community Member
    31 Posts

    Re: Retrieving a list of distinct entries from a DataObject Link to this post

    sorry guys but bumping this one because i really cant find the solution anywhere else. if i haven't supplied enough information then please let me know.

  • Matty Balaam
    Avatar
    Community Member
    69 Posts
  • banal
    Avatar
    Community Member
    901 Posts

    Re: Retrieving a list of distinct entries from a DataObject Link to this post

    You could always use the SQLQuery Class to build your own SQL query for that purpose.
    Something along these lines:

    $sqlQuery = new SQLQuery();
    $sqlQuery->select = array('DISTINCT RequiredRoutewayCourse');
    $sqlQuery->from = array('Job');
    $sqlQuery->where = array($this->ClassName . 'ID = '. $this->ID);
    $result = $sqlQuery->execute();
    foreach($result as $row){
       // do something meaningful with the data... not just print
       print($row['RequiredRoutewayCourse']);
    }

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: Retrieving a list of distinct entries from a DataObject Link to this post

    Is RequiredRoutewayCourse unique for each record or is there a many-to-one relationship there?

    I'm guessing there is otherwise you could just list all entries, have you considered making RequiredRoutewayCourse a DataObject itself? Then you could set up the relationship and just look at the RequiredRoutewayCourse DataObject for your answer, of course, that will affect the Admin use-case.

    Just another way of approaching it.

    Rich

  • nekranox
    Avatar
    Community Member
    31 Posts

    Re: Retrieving a list of distinct entries from a DataObject Link to this post

    thanks guys,

    TotalNet is right that I should make RequiredRoutewayCourse a seperate dataObject of its own with a many-to-one relationship. That would be the most sensible way of modeling the data.

    In this case though I have just used SQLQuery to select all the distinct values from Job and then constructed a dataSet object to pass to a template controller.

    2066 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.