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

Function in Controller or Models


Go to End


2 Posts   864 Views

Avatar
SilverstripeNewbie

Community Member, 14 Posts

20 October 2015 at 3:35pm

Hi, what are the rules on when to put a function on model class or when to put it on controller class?

Avatar
martimiz

Forum Moderator, 1391 Posts

24 October 2015 at 9:05am

Edited: 24/10/2015 9:07am

There are without doubt people who can explain this better, but I'll go and give it a try anyway:

Basically a model (or DataObject) represents the Data in the DataTable, and has all necessary functionality to update its Data (in the CMS) and return its Data, arranged in some way or other.

The controller drives the template, it provides the template with data that it retreives from various Models, and presents in the way the template requests it.

Talking about pages, the current Page_Controller takes care of 'filling' the page's template. That's why its controller methods are accessible from within the template. A page's Model and Controller are special, in that they are aready connected: the Page Controller has acces to its Models methods (and its DataFields), so the template can access them as well.

However, once you enter the scope of a <% loop %> structure, you iterate through Models (DataObjects), so the pages that are returned by Children(), Menu() or DataObject::get() methods are Page objects - NOT Page_Controllers, so you can only access their Page methods. So all necessarey functionality in that case should come from the Model.