Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

DataObject::get('Member') - Foreach Statement.


Reply


4 Posts   2031 Views

Avatar
Puppy

Community Member, 10 Posts

31 August 2010 at 12:47am

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

Avatar
Mad_Clog

Community Member, 78 Posts

31 August 2010 at 1:07am

Edited: 31/08/2010 1:09am

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 %>

Avatar
Puppy

Community Member, 10 Posts

31 August 2010 at 8:42pm

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;
      
   }

Avatar
UncleCheese

Forum Moderator, 4094 Posts

1 September 2010 at 1:46am

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

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