Hi, I’m new to this forum, and I’ve just started to play with CakePHP 3.
I’ve run into a weird problem:
I’m sending an ajax-request (form submit) to the controller, and I expect to get a proper json-response back. It works fine when I set debug mode to false in config/app.php, but when it’s set to true, I get an error-message in the browsers console, and the responsetext seem to be html. I’m calling the action with the .json extension in the url.
I’ve attached a screenshot of the console where the first response is with debug mode set to false, and the second set to true.
I have enabled the extensions in config/routes.php:
Router::scope(‘/’, function (RouteBuilder $routes) {
$routes->extensions(['json', 'xml']);
(…)
Here’s the controller-code:
public function getUserStats() {
$this->log($this->request->data, ‘debug’);if (($this->request->is('post'))) { $this->log('getCategories(): Post-request is received.', 'info'); $usersTable = TableRegistry::get('Users'); $q = $usersTable->find('statsByUsers', $this->request->data); $users = $q->all(); // Calculating total amount per user. foreach ($users as $u) { foreach ($u->purchases as $p) { $u->total += $p->total; } } $this->log($users, 'debug'); $this->set('users', $users); $this->set('_serialize', ['users']); } }
Here’s the model code:
public function findStatsByUsers(Query $query, array $options) {
$options['dates'] = $this->getConvertedDates($options); $query ->contain([ 'Purchases' => function($q) use($options) { return $q ->select(['id', 'total' => 'amount * count', 'purchase_date', 'user_id']) ->where(['purchase_date BETWEEN :fromDate AND :toDate',]) ->bind(':fromDate', $options['dates']['fromDate'], 'datetime') ->bind(':toDate', $options['dates']['toDate'], 'datetime'); } ]) ->where([ 'Users.id IN ' => $options['users'], 'Users.active' => true ]); return $query; }
I hope I’ve given you enough information so that you can help me solve this.
CakePHP version: 3.3.2