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

How to get data from DataObject into template


Go to End


8 Posts   9458 Views

Avatar
coffeespoon

Community Member, 8 Posts

29 November 2011 at 10:23pm

Edited: 29/11/2011 10:26pm

Hi all,

I am really surprised by Silverstripe CMS. I decided to re-code my actual web. I've gone mad from two cases which I am not able to solve (I am sure there is not serious problem) by my very small knowlage of SS.

Be sure I tried to search the doc first, but - no answer on my questions. I am sure I overlooked somethink...

1. there is custom method on controller which calls method from page object

class FacebookEventPage extends Page {
.....
    public function getFbEvent($id) {
        $event = DataObject::get_by_id("FacebookEvent", $id);
        return $event;
    }
..
}
class FacebookEventPage_Controller extends Page_Controller {
...
    public function show() {
        $id = Controller::curr()->urlParams['ID'];
        $this->getFbEvent($id);
        return array();
    }

And also template placed in FacebookEventPage_show.ss
...
<% control event %>
$event.Begin
<% end_control %>
...

I am not able to get result from DataObject::get_by_id("FacebookEvent", $id); to template ..._show.ss
I am sure that function getFbEvent returns some data.

And second question - how to limit (by condition) resultset when DataObject is initiated. I would tried this in __construct method, but Iam sure how.

Many thanks for response and for really very good work you devs made on SilerStripe framework.

Avatar
martimiz

Forum Moderator, 1391 Posts

30 November 2011 at 12:12am

Hi Coffeespoon, welcome to the forums

Your code is a bit incomplete, so I'm not sure what you're tying to accomplish... So first: do you mean to create a pagetype that can show a couple of facebook events per page, or should every page just represent one event?

As for limiting the number of DataObjects returned from a query: you cannot define that in your DataObject, as a DataObject itself represents just one DataObject, one record. Typically you would use the $limit parameter in the DataObject::get() function.

If you haven't yet, I'd really suggest you follow the tutorials (http://doc.silverstripe.org/sapphire/en/tutorials/), as they give you a more thorough insight in the way SilverStripe manages relations...

Martine

Avatar
coffeespoon

Community Member, 8 Posts

30 November 2011 at 1:53am

Hallo martimiz,

thanks for so quick response.

The first you mentioned is right for me. ...pagetype that can show a couple of facebook events per page...
I have one page FacebookEventPage and there is one_to_many relation. Should I send some more lines of code - which.

to limits: sorry with dataobject - you are absolutely right. My problem is, that I would like to show only future events. Is it possible to help where in controller (I guess so) should be this default limitation implemented.

Tutorials are my the best friends in these days :)) also imagegallery, eventcallendar modules are very good friends for inspiration. I tired to reuse pieces of code without success.

Avatar
martimiz

Forum Moderator, 1391 Posts

30 November 2011 at 2:29am

OK, there are more then one ways to do this. One would be to have a EventPage that would just show an event, Then have an EventHolder page that shows all underlying eventpages in some form. For that you could follow the Article/ArticleHolder tutorial in http://doc.silverstripe.org/sapphire/en/tutorials/2-extending-a-basic-site. Also there is an Events module somewhere...

But if you want to use DataObjects for events, you could create a 'FacebookEvent' object first, then have your FacebookEventPage 'has_many' FacebookEvents and your FacbookEvent 'has_one' FacebookEventsPages.

Now in your template you need only:

<% control FacebookEvents %>
	$Title - $EventDate.Nice
	...
<% end_control %>

SilverStripe will automatically return the complete set of FacebookEvents. If you want to restrict, you could add a fuction like this to your page controller (simplified), which the template now will automatically call:

function getFacebookEvents() {
	return DataObject::get('FacebookEvent', 'CURDATE() >= EventDate', 'EventDate DESC');
}

Something like that :-)

Avatar
coffeespoon

Community Member, 8 Posts

30 November 2011 at 2:37am

Edited: 30/11/2011 9:14am

Thank you for your time. I'll go thru your help.

What I missed is behaviour of Holder. I misunderstood that logic. It is necssery to read more carefully.

Avatar
coffeespoon

Community Member, 8 Posts

30 November 2011 at 9:25am

Hi martimiz,

these basics you wrote are OK. I am able to get list of all events. But I would like to click one event for detail and that is the problem for me.
How to implement the detail. I will get detail of one event and how to move the result set to "show" template.

Thanks

Avatar
coffeespoon

Community Member, 8 Posts

1 December 2011 at 2:15am

Avatar
martimiz

Forum Moderator, 1391 Posts

1 December 2011 at 2:34am

Yes, that's a good one! Good luck :-)