Jump to:

7939 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObject::get('Member') - Foreach Statement.

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: 1963 Views
  • Puppy
    Avatar
    Community Member
    10 Posts

    DataObject::get('Member') - Foreach Statement. Link to this post

    Hi everyone,

    I am having problems looping through a members list (DataObjectSet) and would like to ask for a small amount of your time to advise me please.

    Example code of problem:

    OurSupporters.php

    function pullMembers(){
          
          $members = DataObject::get('Member');   
          
          foreach($members as $member){
             return $member;
          }
    }

    An example of the output file:

    OurSupporters.ss

    <h1>$Title</h1>

    <table>
    <th>
    <td>First Name</td>
    <td>Surname</td>
    </th>
    <% control pullMembers %>
    <tr>
       <td>$FirstName</td>
       <td>$Surname</td>
    </tr>
    <% end_control %>
    </table>

    When running it will only display the first member - though it may be a problem with the array so I did try intoArray() but didn't work.

    Though if I remove the foreach statement and "return $members;" it lists all the members but unfortunately it does not help my problem as I need to add each member into a new member class to use inGroup() on a ifelse statement.

    This causes a problem when I wish to find group of a member, like so:

    OurSupporters.php

    function pullMembers(){
          
          $members = DataObject::get('Member');   
          
          foreach($members as $member){

    $privateMember = new Member();
    $privateMember = $member;

    if($privateMember->inGroup(5)){

              return $member;

    }else{
    //Do nothing
    }
          }
    }

    Thanks for your help in advance. I am new to silverstripe and convinced I am overlooking something here...

  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Re: DataObject::get('Member') - Foreach Statement. Link to this post

    Your pullMembers method can only return 1 Member max as you're using return on the first iteration of members.
    The following will loop through all members.
    PHP:

    function pullMembers(){
    $members = DataObject::get('Member');
    return $members;
    }


    SS:

    <% control pullMembers %>
    <tr>
    <td>$FirstName</td>
    <td>$Surname</td>
    </tr>
    <% end_control %>

  • Puppy
    Avatar
    Community Member
    10 Posts

    Re: DataObject::get('Member') - Foreach Statement. Link to this post

    Thanks for your help.

    Now I understand the error was the 'return' in the loop the rest works fine.

    function pullMembers(){
          
          $members = DataObject::get('Member');   
          
          foreach($members as $member){
             
             $unknownMember = new Member();
             $unknownMember = $member;
             
             if($unknownMember->inGroup(5) != true){
                
                $members->remove($unknownMember);   
                
                break;
                
             }
          }
          
          return $members;
          
       }

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObject::get('Member') - Foreach Statement. Link to this post

    $unknownMember = new Member();
    $unknownMember = $member;

    ?!?!?!?!?!?!

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