Database Query as json string

I use this as an example:

$user = $this->find()->toArray();
$this->response->getBody()->write(json_encode($user));
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
Michael

$user = $this->find()->toArray();
this->set(compact(ā€˜userā€™));
$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
https://www.dereuromark.de/2014/01/09/ajax-and-cakephp/

Although in the end he still does a

success: function(response) {
    if (response.content) {
        $('#container').html(response.content);
    }
},

to replace HTML on the page after the AJAX is done so no ā€œrender magicā€ happening here.

1 Like