21310 Posts in 5739 Topics by 2604 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1645 Views |
-
Controllers for DataObjects

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
-
Re: Controllers for DataObjects

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
-
Re: Controllers for DataObjects

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
-
Re: Controllers for DataObjects

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
-
Re: Controllers for DataObjects

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. -
Re: Controllers for DataObjects

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
| 1645 Views | ||
|
Page:
1
|
Go to Top |




