Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Filtering has_many relations from Parent DataObject

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 1855 Views
  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Filtering has_many relations from Parent DataObject Link to this post

    Hello all I have been evaluating SilverStripe to use with our client sites and begun by porting over a simple site, so far so good have all the basics working for the navigation/sub navigation highlighting of current page/section, saerch etc...

    I have been replicating a News/Media Section of the website and managed to get the articles to be listed in their respective categories.

    Also manged to get a list of DISTINCT years as navigation this is where I have run into problems trying to filter the Media by year as I am getting it via Category and the $has_many functionality.

    See below to get what I am trying to do:

    Category.php

    class Category extends DataObject {

       static $db = array(
          'CategoryName' => 'Text'
       );

       static $has_many = array(
          'MediaPages' => 'Media'
       );
    ...

    Media.php

    class Media extends Page {

    static $db = array(
       'MediaTitle' => 'Text',
       'MediaPublished' => 'Date',
       'MediaSummary' => 'Text',
       'MediaArchive' => 'Boolean'
    );

    static $has_one = array(
       'MediaCategory' => 'Category',
       'MediaImage' => 'Image',
       'MediaFile' => 'File'
    );

    ...

    MediaHolder.php

    class MediaHolder extends Page {
    static $db = array(
    );
    static $has_one = array(
    );

    static $default_child = 'Media';

    static $allowed_children = array('Media');
    }

    class MediaHolder_Controller extends Page_Controller {
       
       function Categories()
       {
          if(isset($_GET['y']) && is_numeric($_GET['y'])) {
             
    --> Here is where i need to get the same dataObjectSet as Catgory but need the related Media items filtered by DISTINCT(MediaPublished) = $_GET['y'] <--

          } else {
             return DataObject::get("Category");
          }
       }
       
       function Years()
       {
          // build a new sql query
          $sqlQuery = new SQLQuery();
          $sqlQuery->select = array('YEAR(MediaPublished) AS y');
          $sqlQuery->from = array("Media");
          $sqlQuery->where = array("archive = 0");
          $sqlQuery->distinct = true;
          $sqlQuery->orderby = "y DESC";
          
          $rawSQL = $sqlQuery->sql();
          $result = $sqlQuery->execute();
          
          // create a new data object set
          $myDataObjectSet = new DataObjectSet();
          
          // push our result into the data object set
          foreach($result as $row) {
             $myDataObjectSet->push(new ArrayData($row));
          }

          return $myDataObjectSet;
       }
    }

    ?>

    MediaHolder.ss (Layout)

    <div id="cols">
       <div id="left_col">
          <div id="left_nav">
             <% include MediaMenu %>
          </div>
    </div>
       <div id="right_col">
          <div id="content_header" <% if HeaderImage %> style="background: url($HeaderImage.Filename) no-repeat" <% end_if %>>
             <h2>$Title</h2>
          </div>
          <div id="content">
             <% if Categories %>
                <% control Categories %>
                   <% if MediaPages %>
                      <h3>$CategoryName</h3>
                      <% control MediaPages %>
                         $MediaTitle<br />
                      <% end_control %>
                   <% end_if %>
                <% end_control %>
             <% end_if %>
          </div>
       </div>
    </div>

    MediaMenu.ss (Include)

    <% if Years %>
       <ul>
       <% control Years %>
          <li><a href="$Top.URLSegment?y=$y">$y</a></li>
       <% end_control %>
       </ul>
    <% end_if %>

    This has me stumped most likely I am just approaching this the wrong way?

    Anyways keep up the great work SS team!

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Filtering has_many relations from Parent DataObject Link to this post

    (bump) Anyone give me some pointers please?

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Filtering has_many relations from Parent DataObject Link to this post

    -> Here is where i need to get the same dataObjectSet as Catgory but need the related Media items filtered by DISTINCT(MediaPublished) = $_GET['y'] <--

    I did kinda a similar thing for a recent product - uses a many_many join tho

    // $currentYear = Director::urlParam("ID") -- (yoursite.com/speaker/2 => 2)

    $speakers = DataObject::get("SpeakerPage", '`SpeakerPage_Year`.Year = ' . $currentYear.'"', '', 'LEFT JOIN `SpeakerPage_Years` ON `SpeakerPage`.ID = `SpeakerPage_Years`.SpeakerPageID LEFT JOIN `SpeakerPage_Year` ON `SpeakerPage_Years`.SpeakerPage_YearID = `SpeakerPage_Year`.ID');

    1855 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.