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.

General Questions /

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

Displaying Member profiles


Reply


7 Posts   806 Views

Avatar
DairyPrincess

Community Member, 8 Posts

4 January 2013 at 4:26am

Edited: 04/01/2013 4:44am

I'm trying to get my registered Members from the database and display them in the frontend.
I extended it using this class

<?php

class MyMember extends DataExtension {
   static $db = array(
      'Description' => 'Varchar(250)'
);
   
   static $has_one = array(   
      'Image' => 'Image'
   );

public function getCMSFields() {

    $this->extend('updateCMSFields', $fields);
    return $fields;
}

   public function updateCMSFields(FieldList $fields) {

      $fields->push($imageUpload = new UploadField('Image', 'Profile Image'));
      
      $imageUpload->setFolderName('Profile');
      
      return $fields;
}
}

So now my Members have a profile picture and a Description.
I am able to display the Members details and the image but SEPARATELY
because I don't really know how.
I'm not very familiar with Silverstripe and PHP
please help

this is the function I use to display them
for the picture

   function getPicture(){
      $avatar = File::get()->innerJoin("Member", "\"Rel\".\"ImageID\" = \"File\".\"ID\"", "REl");
      return $avatar;
   }


for the Members detail

   function pullMembers(){
      $members = DataObject::get('Member');
      //$avatar = DataObject::get_by_id('Image', $members->ImageID);
      //return $members;
      return $members;
   }


the templat

<div class="content-container">   
   <article>
      <h1>$Title</h1>
      <div class="content">
         $Content
         
         <% control getPicture %>    
         
            <img style="width: 50px" src="$filename"/>
         
         <% end_control %>
         
         <% control pullMembers %>
            <ul>   
            <li><p><b>$Title</b></p></li>
            <p>$Email</p>
            <p>$Description<p>
            </ul>
         <% end_control %>
         
      </div>
   </article>
      $Form
</div>
<% include SideBar %>

I would like to display their image together with the Members detail

Avatar
copernican

Community Member, 189 Posts

4 January 2013 at 6:10am

Edited: 04/01/2013 8:13am

Hi DairyPrincess

Thanks to SilverStripe, there's no need to setup a custom function to retrieve the picture. You'll also want to use DataList instead of DataObject::get() if this is a SS v3.0 project.

your new function to get members

public function getMembers(){
//make sure members exist.
if(DataList::create('Member')->count() >= 1){
return DataList::create('Member');
}
}

and in your template you should use <% loop %> not <% control %> permitting this is a v3.0 project. as an example

<% if $Members %>
<% loop $Members %>
$Image.setWidth(125)
<% end_loop %>
<ul>
<% loop $Members %>
<li>$Email</li>
<li>$Description</li>
<% end_loop %>
</ul>
<% end_if %>

Avatar
DairyPrincess

Community Member, 8 Posts

4 January 2013 at 7:30am

Edited: 04/01/2013 7:31am

thanks for the reply!
I tried your code but I'm getting a server error
I am using the 3.0.3 version
somehow I'm not getting errors about deprecated classes
not sure why haha
I just find out that something's wrong when I get a server error

Avatar
copernican

Community Member, 189 Posts

4 January 2013 at 8:15am

Edited: 04/01/2013 8:15am

There was a typo in my code (fixed now) i had DataList::create('Members'), when it should probably be DataList::create('Member');

if you still get errors after making that change let me know what they are.

Avatar
DairyPrincess

Community Member, 8 Posts

4 January 2013 at 4:36pm

Edited: 04/01/2013 5:16pm

Thank you!
It works now :D

I'm sorry but how do I display the members that belong to a specific group?
ex: all of the members that belong to Administrators
how do I use this

DataList::Create("Group")->relation("Members")

Avatar
copernican

Community Member, 189 Posts

5 January 2013 at 2:25am

If you want to display members from a certain group, I use something like

$group = DataList::create('Group')->byID('x') //x is the ID of the group you want
return $group->Members();

or you could use something like

$group = DataList::create("Group")->where("Title = 'Administrators'")->first();
return $group->Members();

Avatar
DairyPrincess

Community Member, 8 Posts

5 January 2013 at 4:53am

works great! thanks so much :D