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.

Archive /

Our old forums are still available as a read-only archive.

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

How to call controller functions from the model?


Go to End


5 Posts   5209 Views

Avatar
Allisone

Community Member, 27 Posts

8 December 2008 at 2:25pm

Edited: 08/12/2008 2:25pm

Hey guys,
I have something I can't solve
<% control Children %>
gives me back all my children
And I can call all the methods of all those children,
but I can't reach the methods of the controller of the children
Is there a way to do that ?

Avatar
Allisone

Community Member, 27 Posts

8 December 2008 at 6:40pm

I tried further and further
Also tried to call the model from the controller
Isn't there are way
to get the model related to the controller with a call from a controller function and the controller related to the model calling from a model function ?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 December 2008 at 3:50am

If you need to do that, there's something wrong with your design. The controller is responsible for everything that happens in the view (template). But the model is responsible for manipulating and retrieving records from the database.

As a general rule, if the function is going to be used in more than one template, put it in the model. Otherwise, it goes in the controller. The template, you'll notice, can actually call functions from either class.

Avatar
Allisone

Community Member, 27 Posts

9 December 2008 at 4:28am

Edited: 09/12/2008 4:30am

Thanks for the anwer :D
Well till now I guess my design isn't that well thought-out, that's true.

To what you are saying. I would love to call the controller function from the template, but I can't reach the childrens controller. This below doesn't work.

<% control Children %>
<% control AChildFunctionInChildController %>
<% end_control %>
<% end_control %>

Thus I thought, maybe I can reach the childrens controller functions through a Model function of the child
Or does this wish also mean that my design must be crap ^^ ?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 December 2008 at 4:36am

Yeah, when you loop through the children, you're accessing them as a model, not as a controller. The controller is a set of functions for only the current template. Just move the functions you want to use in those objects from their controller to the model, and you should be fine.