Database Query as json string

I use this as an example:

$user = $this->find()->toArray();
return $this->response;

Is there a better way to transform an array result or an entity object direct into a json string??

Thanks for a hint or help

$user = $this->find()->toArray();
$this->viewBuilder()->setOption(‘serialize’, ‘user’);

I use cake 3.1. In controller:

$this->autoRender = false;
echo json_encode($user);

@jarekgol, you should never echo your output directly.

I do it regularly and it works, so why?

You are skipping the whole View part of the MVC framework with what you are doing.

So you disable the auto-rendering part and manually do the view rendering part in your controller which violates the MVC paradigm

We recommend you use the JSON view or in your specific case (because you are on such an old CakePHP version) go with this JSON view doc since you still have to use the old RequestHandler component.

I agree with @Zuluru, must never echo out directly

I’m not super familiar with all those many conventions, but as for MVC:
I’ve got some data-base related definitions in Model, I’ve got some queries in Controller and I pass it to Template via $this->set, which template is mostly in php and html, which then goes from apache to my browser in simple case via old good known get request. That is working form me and I think I understand it.

And now if I want to use some other kind o view, I need some JS which will handle all data transfer and rendering? Am I right?

Yes, the base principle is

  • models are PHP representations of your DB tables,
  • controllers are called from requests to fetch data via the models and then pass it on to the
  • view which is responsible to render the previously fetched (and maybe transformed) data from the controller.

In your case you say you pass on query objects from the controller to the view via $this->set which is 100% true.

BUT you need to understand that CakePHP does its magic when you iterate over that query inside the template that it actually executes the query and transforms the query object into an array of entities.

How this all connects together with a JSON endpoint will be explained in my next video which will come up very soon.

1 Like

I still don’t get how it is rendered at browser side. Let’s say I manage to send JSON from my server one way or another, and then what? Is there some built mechanics in cake which will receive this at browser site and render somehow? I’m only use JS XMLHttpRequest as helpers/addition, for example to find some stuff while typing, without sending whole form back and forth. And for this purpouse making separate method in Controller which receive this requests and send back results in JSON via echo json_encode($query->toArray()); works fine, and seems to me as an easiest way.

CakePHP comes with a very barebone HTML and CSS setup where you are required to built your frontend however you like. This also means, that there is no JS logic whatsoever to automatically load/render JSON APIs. You have to build that yourself (just like you manually added those AJAX/XMLHttpRequest calls in your JS files).

But you can take a look at what dereuromark does here with his ajax plugin

Although in the end he still does a

success: function(response) {
    if (response.content) {

to replace HTML on the page after the AJAX is done so no “render magic” happening here.

1 Like