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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

[Solved] Get a value from has_one in controller


Go to End


5 Posts   481 Views

Avatar
Vlad Belfort

Community Member, 55 Posts

15 April 2016 at 1:31am

Edited: 15/04/2016 1:41am

I don't understand what I'm doing incorrectly when trying to get just 1 column of data with a has_one relationship...

What I have is a page type with a GridField that allows users to assign people to it. After they're assigned their user ID is stored relating to that page, I need to use that ID to get their Firstname, Surname, Photo etc from the Member table and display it in the template.

These are the entries in the database for the GridField table storing assigned people.

+----+--------------+------------------+----------+----------+
| ID | ClassName    | Role             | MemberID |   ShowID |
+----+--------------+------------------+----------+----------+
|  1 | ShowsContact | CG Supervisor    |     5927 |       79 |
|  2 | ShowsContact | Dept Coordinator |     5927 |       79 |
|  3 | ShowsContact | CG Supervisor    |     5927 |       79 |
|  4 | ShowsContact | CG Supervisor    |     5927 |       78 |
|  5 | ShowsContact | VFX Editor       |     7085 |       78 |
|  6 | ShowsContact | Unit PA          |     6473 |       78 |
+----+--------------+------------------+----------+----------+

If I do:
return $this->ShowsContacts();

I can retrieve Role and MemberID in the template but I can't figure out how to just get the MemberID in the controller so I can search for that in Member and get that users details then display it in the template.

If I use anything like

$this->ShowsContacts() #...
it doesn't work. How do I get the MemberID in the controller?

Avatar
Vlad Belfort

Community Member, 55 Posts

15 April 2016 at 2:31am

Edited: 15/04/2016 2:36am

I've got something that does what I need. An array list that contains all the relevant info.

    foreach($this->ShowsContacts()->column('MemberID') as $x){
        echo $x . '<br>'; # just to test output...
    };

Would looping over this result set and querying the Member table be the best way of getting the data and displaying it?

Avatar
Vlad Belfort

Community Member, 55 Posts

15 April 2016 at 5:01am

Edited: 15/04/2016 5:05am

So now I've got this which when I use echo it outputs the members names onto the page but how do I access it properly within the template?

I've tried setting a variable and using that variable within a loop but nothing happens...

I've also tried using array_push and returning the array but that didn't work either :|

if($this->ShowsContacts()->exists()){

    # $result = array(); was testing array_push

    foreach($this->ShowsContacts()->column('MemberID') as $teamMemberID){

        # echo Member::get()->byID($teamMemberID)->Nickname . '<br>'; doing it like this works, 
        # prints out all names at the top of the page

        # just testing trying to get at least 1 name out to the template
        $x = Member::get()->byID($teamMemberID);
        $x = $x->Nickname;
        return $x;
    }

    #return $result; array_push test
}

Avatar
Vlad Belfort

Community Member, 55 Posts

16 April 2016 at 2:40am

This is what I have so far in my controller. This works and gets the result set I need but I still can't get it to display in the template...

 $result = new ArrayList();

 foreach($this->ShowsContacts()->column('MemberID') as $teamMemberID){
    $result->add(Member::get()->byID($teamMemberID)->Nickname);
 }

 var_dump($result);

Avatar
Vlad Belfort

Community Member, 55 Posts

16 April 2016 at 5:00am