Jump to:

23371 Posts in 18161 Topics by 2865 members

General Questions

SilverStripe Forums » General Questions » newbe question: contentcontroller versus dataobject

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

Page: 1
Go to End
Author Topic: 1066 Views
  • yurigoul
    Avatar
    Community Member
    202 Posts

    newbe question: contentcontroller versus dataobject Link to this post

    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?

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: newbe question: contentcontroller versus dataobject Link to this post

    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

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: newbe question: contentcontroller versus dataobject Link to this post

    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).

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: newbe question: contentcontroller versus dataobject Link to this post

    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.

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: newbe question: contentcontroller versus dataobject Link to this post

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

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: newbe question: contentcontroller versus dataobject Link to this post

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

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: newbe question: contentcontroller versus dataobject Link to this post

    Ah, I see, thanks for clearing that up for me

    1066 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.