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
Try
$Category->toJson()
;)
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
anybody? nobody? help me plase
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);
}
}
1 to 5 of 5