Akelos Framework v1 forum archive. This forum is no longer maintained. To report bugs please visit https://github.com/akelos/akelos/issues
    • CommentAuthorvektor
     

    Hi all,

    I have a very simple project, and I would like to render my entire Category model in JSON. I was already tryed a lot of approach but all of them was unsuccessfull. I hope you can help me to find the right way.

    regards

    Viktor

    •  
      CommentAuthorbermi
     

    Try

    $Category->toJson()
    

    ;)

    • CommentAuthorvektor
     

    Bermi thank you, for the fast answer. I was already tryed it, but it only represents only the structure itself not with the data.
    Oh I was wrong in my previous comment, I need to represent the associations too not just one model.

    in category model:

    var $has_many = 'topics';
    

    in topic model:

    var $belongs_to = 'category';
    

    and in my sync controller:

    function index(){
        $this->topics = $this->Topic->find('all', array('include' => 'category'));
        //$Category->toJson();
        $this->renderText($this->Topic->toJson());
    }
    

    Could you write it down in this environment with an example please? Maybe I need to make a view with a builder for complex structures? And if yes, then how?

    thanks

    • CommentAuthorvektor
     

    anybody? nobody? help me plase

    •  
      CommentAuthorbermi
     

    You need to convert to json the array of results $this->topics, not the instance at $this->Topic

    Try with something like:

    function index(){
      if($this->topics = $this->Topic->find('all', array('include' => 'category'))){
        foreach ($this->topics as $k => $Topic){
          $this->topics[$k] = $Topic->toJson();
        }
        $this->renderText('['.join(',', $this->topics).']');
      }else{
        $this->renderText('[]', 404);
      }
    }