Jump to:

10993 Posts in 2727 Topics by 1818 members

All other Modules

SilverStripe Forums » All other Modules » DataObject to JSON format

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 3577 Views
  • draft
    Avatar
    Community Member
    57 Posts

    DataObject to JSON format Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: DataObject to JSON format Link to this post

    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.

  • draft
    Avatar
    Community Member
    57 Posts

    Re: DataObject to JSON format Link to this post

    $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?

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: DataObject to JSON format Link to this post

    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.

  • joninjas
    Avatar
    Community Member
    32 Posts

    Re: DataObject to JSON format Link to this post

    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

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