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.

All other Modules /

Discuss all other Modules here.

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

DataObject to JSON format


Go to End


5 Posts   7216 Views

Avatar
draft

Community Member, 57 Posts

3 June 2010 at 4:28pm

hi guys,

i read in one of old topic to create json from dataobject is with this function :
$f = new JSONDataFormatter();
return $f->convertDataObject($myDataObject);

but it's not working, it gave me this error :
[User Error] Uncaught Exception: Object->__call(): the method 'inheriteddatabasefields' does not exist on 'DataObjectSet'

Please someone tell me the correct way to create json format from DataObject

Avatar
Willr

Forum Moderator, 5523 Posts

3 June 2010 at 10:08pm

Hmm can you post your full code and the full backtrace of the error. Seems a bit weird that a method is being called on a 'DataObjectSet' if your performing a function on dataobject.

Avatar
draft

Community Member, 57 Posts

4 June 2010 at 3:23am

$data = DataObject::get("TABLE","WHERE");
$f = new JSONDataFormatter();
return $f->convertDataObject($data);

It seem like the problem is the JSONDataFormatter is only work for single dataobject, if i select the dataobject using get_one or get_by_id it's working, but what i want to achieve is to hv more than one data as JSON.

Is there anything i can do?

Avatar
dhensby

Community Member, 253 Posts

2 October 2010 at 3:06am

Just for anyone that stumbles across this and wants the answer.

You have to use convertDataObjectSet($dos) on a dataobjectset. convertDataObject is just for a single DataObject.

Avatar
joninjas

Community Member, 32 Posts

14 December 2010 at 8:49am

Thanks for your help! I can now pass a JSON object to the flash.swf, really handy.

But a problem still exists:
http://www.depot43.ch/silverflash/flashinhalte/textfelder/textfeld1/jsonausgabe only displays with the variables from the TextFeldangabe table when I'm logged in the backend. Would there be a possibility to log in a readonly user automatically in the controller or something like that??

class TextFeldPage_Controller extends Page_Controller {


           public function jsonausgabe() {
           $data1 = DataObject::get("TextFeldangabe","TextFeldangabe.Titel='$this->URLSegment'");
           $f1 = new JSONDataFormatter();
           return $f1->convertDataObjectSet($data1);
           }

}

Thanks for your help mates!!!
JoNinjas

Please answer to thread http://silverstripe.org/all-other-modules/show/15161