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.

General Questions /

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

Controllers for DataObjects


Reply


7 Posts   2299 Views

Avatar
Mo

Community Member, 510 Posts

13 July 2010 at 1:16am

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

Avatar
mark_s

Community Member, 78 Posts

13 July 2010 at 9:11am

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

Avatar
Mo

Community Member, 510 Posts

13 July 2010 at 9:34am

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

Avatar
Mo

Community Member, 510 Posts

1 September 2010 at 11:22pm

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

Avatar
Mad_Clog

Community Member, 78 Posts

1 September 2010 at 11:29pm

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

Avatar
Mo

Community Member, 510 Posts

2 September 2010 at 12:55am

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

Avatar
nimeso

Community Member, 17 Posts

5 November 2010 at 8:58am

I have same issue?