Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Filtering DataOject::get

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

Page: 1
Go to End
Author Topic: 931 Views
  • VictorH
    Avatar
    Community Member
    29 Posts

    Filtering DataOject::get Link to this post

    I have a Profile class and a CaseStudy class. I've created a man_many relationship between the two and I'm trying to display the CaseStudies that belong to the Profile page. So far I have:

    function ProfileCaseStudies() {
          return DataObject::get('CaseStudy', "", '', '', '');
       }

    which pulls all the CaseStudies. How do I filter it so its only pulling the CaseStudies that belong to the specific Profile.

    Full Code

    Profile.php

    <?php

    class Profile extends Page {
       static $db = array(
          'Summary' => 'Text'
       );
       
       static $has_one = array(
          'Photo' => 'Image'
       );
       
       static $many_many = array(
          'CaseStudies' => 'CaseStudy'
       );

       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $fields->addFieldToTab('Root.Content.Main', new TextField('Summary'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new ImageField('Photo'), 'Content');
          
          
          $profileList = new ManyManyComplexTableField(
             $this,
             'CaseStudies',
             'CaseStudy',
             array(
                'Title' => 'Title'
             ),
             'getCMSFields_forPopup'
          );
          
          $fields->addFieldToTab('Root.Content.Categories', $profileList);
          
          return $fields;
       }
    }

    class Profile_Controller extends Page_Controller {
       function ProfileCaseStudies() {
          return DataObject::get('CaseStudy', "", '', '', '');
       }
    }

    ?>

    CaseStudy.php

    <?php

    class CaseStudy extends Page {
       static $db = array(
          'Summary' => 'Varchar',
          'Featured' => 'Boolean'
       );
       
       static $many_many = array(
          'Categories' => 'Category'
       );
       
       static $belongs_many_many = array(
          'Profiles' => 'Profile'
       );
       
       static $default_parent = 'CaseStudyCategory';

       function getCMSFields() {
          $fields = parent::getCMSFields();
          
          $fields->addFieldToTab('Root.Content.Main', new TextField('Summary'), 'Content');
          $fields->addFieldToTab('Root.Content.Main', new CheckboxField('Featured'), 'Content');
          
          
          $categoryList = new HasManyComplexTableField(
             $this,
             'Categories',
             'Category',
             array(
                'Name' => 'Name'
             ),
             'getCMSFields_forPopup'
          );
          
          $fields->addFieldToTab('Root.Content.Categories', $categoryList);
          
          return $fields;
       }
    }

    class CaseStudy_Controller extends Page_Controller {
    }

    ?>

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Filtering DataOject::get Link to this post

    Hi Victor,

    For something like that I don't think you need to write a new function - if you call the name of the relationship in the ProfilePage template it should return the related CaseStudies as a dataobjectset.

    so if you have $many_many = array( 'CaseStudies' => 'CaseStudy') on your Profile class try this in your Profile.ss

    <% control CaseStudies %>
    //Case study properties, $Title etc.
    <% end_control %>

    If you want to use that dataobjectset in a custom function you could use $this->CaseStudies() from your controller

    Cheers

  • VictorH
    Avatar
    Community Member
    29 Posts

    Re: Filtering DataOject::get Link to this post

    Thank you so much JonoM it was killing me and it was such a simple solution that flew right by me.

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Filtering DataOject::get Link to this post

    You're welcome, I know that feeling!

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