Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Access to $many_many Object(s) from Within $belongs_many_many Object(s)

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 662 Views
  • Garrett
    Avatar
    Community Member
    245 Posts

    Access to $many_many Object(s) from Within $belongs_many_many Object(s) Link to this post

    Hi,

    I have a group of pages -A- which has a $has_many relation to a set of DataObjects -B-.

    I am in a nested loop showing all of the -B- objects which are associated with each -A-.

    Now, -B- also has a $belongs_many_many relation to another page type -C- (which in turn has a $many_many relation to -B-).

    Do I have access to the set of -C- objects associated with each -B- in the loop in any way? Would make sense that I would, I just can't figure out how I would refer to that in the template. I am trying to avoid writing a custom query and I can't pass an ID back to the PHP from the template.

    -A-:
    class Service extends Page {

       static $has_many = array(
          "SubServices" => "SubService"
       );

    -B-:
    class SubService extends DataObject {
       
       static $has_one = array(
          "Service" => "Service"      
       );

       static $belongs_many_many = array(
          "CaseStudy" => "CaseStudy"
       );

    -C-:
    class CaseStudy extends Page {
       
       static $many_many = array(
        "SubServices" => "SubService"
       );

    Guidance would be appreciated. I've never had this exact kind of data relation before.

    Thanks,
    Garrett

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Access to $many_many Object(s) from Within $belongs_many_many Object(s) Link to this post

    On a service 'A'

    <% control SubServices %>
    In a 'B'
    <% control CaseStudy %>
    In a 'C'
    <% end_control %>
    <% end_control %>

    Or to you want to bypass the second control? In that case you'd need a custom query.

  • Garrett
    Avatar
    Community Member
    245 Posts

    Re: Access to $many_many Object(s) from Within $belongs_many_many Object(s) Link to this post

    @Willr...... Once again...... you are my knight in shining armor!

    This works brilliantly. I am now seeing all the Case Studies which are associated with a Service THROUGH a Subservice. Couldn't be happier.

    What's funny is that this very approach was my instinct but there is something unintuitive about a control loop around a non-plural term, you know? I.e.: "CaseStudy" vs. "CaseStudies" as you would do in the outer loop. But I knew CaseStudies wouldn't work because there's no reference to that anywhere. Thank you so much, my good man. If I ever come to NZ you drink for free.

    Now that I have this I will not require the solution to my other post from the same day but will file that snippet in my toolbox. Thanks for that too.

    //Garrett

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Access to $many_many Object(s) from Within $belongs_many_many Object(s) Link to this post

    What's funny is that this very approach was my instinct but there is something unintuitive about a control loop around a non-plural term, you know? I.e.: "CaseStudy" vs. "CaseStudies"

    Well normally you name the relationship with a plural...

    -B-:
    class SubService extends DataObject {

    static $has_one = array(
    "Service" => "Service"
    );

    static $belongs_many_many = array(
    "CaseStudies" => "CaseStudy" // <-- note case studies
    );

    -C-:
    class CaseStudy extends Page {

    static $many_many = array(
    "SubServices" => "SubService"
    );

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