Jump to:

22996 Posts in 11844 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Controllers for DataObjects

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: 2054 Views
  • Mo
    Avatar
    Community Member
    485 Posts

    Controllers for DataObjects Link to this post

    Hi All,

    This is a question I have been pondering for a while. Is it possible to create Controllers for use with standard DataObjects? If so, would I just create one like so:

    class MyObject extends DataObject {
    ...
    }

    class MyObject extends Controller {
    ...
    }

    Would that deal with relations and sharing of variables properly?

    Also, if you can assign a controller to a DataObject, how can I access it in my template? Will its methods be automatically be available when the dataobject is called (Via a control etc)?

    Hope that makes sense,

    Cheers,

    Mo

  • mark_s
    Avatar
    Community Member
    78 Posts

    Re: Controllers for DataObjects Link to this post

    Hi Mo.

    Better to use ContentController, which is designed to work with a data object instance. Page_Controller is an subclass of ContentController. A ContentController instance also uses it's data object as a failover object. This is very useful, because you can refer to properties and methods of the data object instance directly in the template - if the ContentController doesn't understand the method or property, it passes it to the data object.

    Mark

  • Mo
    Avatar
    Community Member
    485 Posts

    Re: Controllers for DataObjects Link to this post

    Oh ok, that makes sense.

    I wasn't sure if Sapphire was fully MVC in this respect, or if it was just Sitetree and Widgets that adhered to that structure.

    Can't wait to play around with this .

    Cheers,

    Mo

  • Mo
    Avatar
    Community Member
    485 Posts

    Re: Controllers for DataObjects Link to this post

    I have been trying to get this working, but don't seem to be having much luck.

    I have a DataObject and a controller for that object, which I am rendering out in an associated Page template. All methods on the object render fine, but if I try and add a method to the controller, it isn't recognised.

    Am I supposed to tie these together somehow manually?

    Any thoughts?

    Cheers,

    Mo

  • Mad_Clog
    Avatar
    Community Member
    78 Posts

    Re: Controllers for DataObjects Link to this post

    Could you put your actual code on pastie or something similar.
    Would be much easier to give some feedback on.

  • Mo
    Avatar
    Community Member
    485 Posts

    Re: Controllers for DataObjects Link to this post

    Ok, I have tried to simplify the classes, and cut out everything that isn't necessary. Hopefully you will get an idea of what I am trying to do.

    This is in Conversation.php:

    class Conversation extends DataObject {
    ...
    public static $has_one = array(
    'Parent' => 'Page'
    );

    public static $many_many = array(
    'Attachments' => 'File'
    );
    ...
    }

    class Conversation_Controller extends ContentController {
    public function init() {
    parent::init();
    }

    public function getAttachment() {
    // returns some code
    }
    }

    This is in my Page.ss

    class Page extends SiteTree {
    ...
    }

    class Page_Controller extends ContentController {
    ...

    public function init() {
    parent::init();
    }

    public function getConversation() {
    return DataObject::get_by_id('Conversation',$this->urlParams['ID']))
    }
    }

    Finally in the template I have:

    <% if Conversation %>
    <% control Conversation %>
    $getAttachment.Link
    <% end_control %>
    <% end_if %>

    Cheers,

    Mo

  • nimeso
    Avatar
    Community Member
    17 Posts

    Re: Controllers for DataObjects Link to this post

    I have same issue?

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