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

newbe question: contentcontroller versus dataobject


Go to End


7 Posts   1460 Views

Avatar
yurigoul

Community Member, 203 Posts

8 November 2009 at 11:10pm

This has probably been asked before, but I could not find a solid link for this. According to http://doc.silverstripe.org/doku.php?id=page-types

- page_controller is the controler. Functions defined on the controller are only available when you actually visit the page
- page is the dataobject. Any method on the dataobject is available whenever the object is referenced in the system

I am wondering what goes where and why and what the benefits are of placing it in one or the other.

As an example: Since the children of my Page-class share common methods I define them in the Page.php. But: I have discovered certain functions can be placed in the dataobject or in the controller where others have to be in the dataobject

1. I have a function that extracts a shorter version of the Content field so it can be displayed in another page with a 'do you want to read more'-link. I can understand that this can ONLY be in the dataobject because I have to extract the data from that page even when the page is not visited itself.

2. I extract the data for the footer of every page from a page called 'Footer' of class 'FooterOfPage' using DataObject::get_one('FooterOfPage'). Wit this method I do not access the page, I access the database. I can put that method in the controller OR in the dataobject. Is there a difference? If I put it in the dataobject, I can also access the function when in the cms - if I have to. Is that correct? Is that the only difference?

Avatar
Carbon Crayon

Community Member, 598 Posts

9 November 2009 at 2:00am

Edited: 09/11/2009 2:01am

Hi yurigoul

The 'Page' class is actually refered to as the 'Model', although it is an extention of dataobject, is has more to it, such as version control. A dataobject is a simpler class, which are used to attache objects to pages when those objects do not need their own page in the site, so a good example would be testimonials attached to a testimonials page.

Anyway the answer to the question of Model vs Controller comes down to how you want to use the data. For a footer where you only have a single pages data that you want to use, it's simple enough to add a funtion to every pages controller to return that page and use it.

However if you have a load of article pages for example that you want to summarise on another page, it is FAR easier to be able to have a function in your controller which returns all the article pages then in your template be able to call a function on each one of those articles inside the <% control %> loop to return the Summary. In order to be able to do that, the function must be in the ArticlePage model.

If it wasnt you would ahve to do some very complex processing to the the Articles that are returned in the Page_controller to add the summary as part of the DataObjectSet that is returned.

I hope that is somewhat clear, it is a subtle difference, one of those things that as you practice and find more use cases for each you will understand it more.

Aram

Avatar
yurigoul

Community Member, 203 Posts

9 November 2009 at 3:58am

Summary -> ? // true | false

Controller: is where you put the functions that returns stuff from other pages, provided you do not need to access that stuff from yet another page.
- For example: a function that returns the 5 newest articles on your homepage

Model: is where you put the functions that need to be accessed from other pages, even when it returns stuff that belongs to yet another page.
- for example: a function that returns the first 100 words from your articles will be placed in the model when it needs to be accessed from your homepage. And if you want to be able to also show the section (= parent- or grandparent-page) this article belongs to on your homepage, you also put the function that returns that info in the same model of the same article (provided you can not access this using $parent.SomeInfo - possible bad example error).

Avatar
ajshort

Community Member, 244 Posts

9 November 2009 at 10:50am

Actually it's really simple: you should put your model/data layer methods in the model class - this includes the method that returns the first 100 words of articles. All the request handling, actions, form logic etc. is placed in the Controller.

Avatar
yurigoul

Community Member, 203 Posts

9 November 2009 at 11:46am

Should the method that returns the latest 5 news articles then also be placed in the model?

Avatar
ajshort

Community Member, 244 Posts

9 November 2009 at 12:11pm

Probably, but to be honest there will be people with different viewpoints and the whole thing is often a much of a muchness.

Avatar
yurigoul

Community Member, 203 Posts

9 November 2009 at 12:20pm

Ah, I see, thanks for clearing that up for me :-)