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.

All other Modules /

Discuss all other Modules here.

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

DataObject to JSON format


Reply


5 Posts   3691 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, 5513 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
Pigeon

Community Member, 243 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:
[url]http://www.depot43.ch/silverflash/flashinhalte/textfelder/textfeld1/jsonausgabe[/url] 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