Jump to:

23550 Posts in 19336 Topics by 2890 members

General Questions

SilverStripe Forums » General Questions » Multiple levels of getManyManyComponents

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 735 Views
  • dendeffe
    Avatar
    Community Member
    135 Posts

    Multiple levels of getManyManyComponents Link to this post

    How can I do a getManyManyComponents for each DO in a DataObjectSet and bundle the result in one DataObjectSet?

    Something like $this->getManyManyComponents('DOType1')->getManyManyComponents('DOType1');

    I can do it by looping through the first set with a for each loop, but is there an easier way?

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: Multiple levels of getManyManyComponents Link to this post

    how about a recursive function... I've started with some function that might help, but I'm lost as to wehter it;ll work and what the result you are after is...

    function GetRecursiveAllObjects($obj,$strRelationshipName) {
    while ($obj->hasMethod($strRelationshipName)) {
    $dos->merge($obj->$strRelationshipName);
    $obj = ?;
    $this->GetRecursiveAllObjects($obj,$strRelationshipName);
    }
    }

    edit updated the function a little

  • dendeffe
    Avatar
    Community Member
    135 Posts

    Re: Multiple levels of getManyManyComponents Link to this post

    I'm not sure if that is cleaner/more efficient then what I'm doing at the moment. This is what I have:

       function Recipients()
       {
          $emailsOnList = array();
          $myLists = $this->DefNewsletterSubscriberLists();
          $myLists = $myLists->items;
          // loop through mailing lists
          for($x = 0; $x < count($myLists); $x++)
          {
             $myEmails = $myLists[$x]->getManyManyComponents('DefNewsletterSubscribers');
             $myEmails = $myEmails->items;
             // loop emails
             for($y = 0; $y < count($myEmails); $y++) {
                $emailsOnList[] = $myEmails[$y]->Email;
             }
          }
       
          $emailsOnList = array_unique($emailsOnList);
          return $emailsOnList;
       }

    I'll probably look into returning a DataObjectSet instead of an Array. Was hoping to do something more DataObject-like instead of the for loop.
    I'm also concerned if this is good performance-wise.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Multiple levels of getManyManyComponents Link to this post

    Performance wise you're better off looping through arrays like you're doing than traversing dataobjectsets. Arrays are generally much faster than object iterators. I've also read that recursive functions are slow in PHP but I don't have anything to back that up with so don't take my word for it.

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: Multiple levels of getManyManyComponents Link to this post

    you know, I thought it you wanted it to keep going recursively...

    As I understand it (although I could be badly wrong here) doing a foreach on a dataobjectset invokes it's ->toArray() method and then it iterates over that... besides I optimize when it's required in the 1% of cases in the 99% I prefer succinct readable code.

  • MarcusDalgren
    Avatar
    Community Member
    288 Posts

    Re: Multiple levels of getManyManyComponents Link to this post

    I agree with you Swaiba on performance vs readability but I do want to add that (at least for me) recursive functions are seldom readable in an easy fashion. Your function is very short so that one's easy to read but at least in my experience recursive functions often get messy pretty fast. The reason I wrote about performance was simply because Dendeffe asked about it.

    For the foreach I think it's using the iterator interface defined in the class and thus each foreach calls one of those methods. Those methods do work directly with the internal array but I do think you still get the overhead of calling the object methods. But like Swaiba said in 99% of the cases this shouldn't be the overhead in your application so it isn't really something to worry about unless you're working with huge data sets.

  • swaiba
    Avatar
    Forum Moderator
    1796 Posts

    Re: Multiple levels of getManyManyComponents Link to this post

    You know I haven't written a recursive function for years and then yesterday I wrote 2... hopefully it'll be years before I do again!

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