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.

Template Questions /

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

Retreiving Data Model inside a Template


Go to End


2 Posts   1267 Views

Avatar
Ajaxian

Community Member, 8 Posts

2 December 2012 at 12:58am

Edited: 02/12/2012 12:59am

Hi All,

I've 3 models A has_many B, B has_many C; B has_one A, C has_one B

In a template file I can get all C models/instances. But I only can address the attributes of C, I can't get Attributes of the B instance with which A has a has_one relationship.

The only way I see is to have a method in C model:
public function getNameofB() {
return B::get()->get_ByID($this->BID)->Name
}

But is there another way of doing this. ?

Thanks

Avatar
Willr

Forum Moderator, 5523 Posts

2 December 2012 at 5:17pm

You can use <% loop %> and <% with %> on relations so from C you can do - <% with B %>$Name<% end_with %>..