Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » How to call controller functions from the model?

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

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

Page: 1
Go to End
Author Topic: 3377 Views
  • Allisone
    Avatar
    Community Member
    27 Posts

    How to call controller functions from the model? Link to this post

    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 ?

  • Allisone
    Avatar
    Community Member
    27 Posts

    Re: How to call controller functions from the model? Link to this post

    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 ?

  • UncleCheese
    Avatar
    4085 Posts

    Re: How to call controller functions from the model? Link to this post

    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.

  • Allisone
    Avatar
    Community Member
    27 Posts

    Re: How to call controller functions from the model? Link to this post

    Thanks for the anwer
    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 ^^ ?

  • UncleCheese
    Avatar
    4085 Posts

    Re: How to call controller functions from the model? Link to this post

    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.

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