I에 유래하는 새로운 해요, 난 그냥 CakePHP는 놀러 시작했습니다 3.CakePHP의 3 - 적절한 JSON을 반환 할 수없는 경우 디버그 모드 = 사실
내가 이상한 문제로 실행했습니다 :
저는 컨트롤러에 ajax 요청 (양식 제출)을 보내고 있습니다. 적절한 json 응답을 되 찾을 것을 기대합니다. config/app.php에서 디버그 모드를 false로 설정하면 제대로 작동하지만, true로 설정되면 브라우저 콘솔에 오류 메시지가 표시되고 responsetext는 html로 보입니다. URL에서 .json 확장자로 액션을 호출합니다.
내가 참으로 첫 번째 응답이 false로 설정된 디버그 모드 인 콘솔, 두 번째 세트의 스크린 샷을 연결 한 : 내가 설정/경로의 확장을 활성화 한
. PHP :
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']);
}
}
,536 : 여기
Router::scope('/', function (RouteBuilder $routes) {
$routes->extensions(['json', 'xml']);
(...)
컨트롤러 코드입니다
public function findStatsByUsers(Query $query, array $options) {
debug($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') // Binds the dates to the variables in where-conditions
->bind(':toDate', $options['dates']['toDate'], 'datetime');
}
])
->where([
'Users.id IN ' => $options['users'],
'Users.active' => true
]);
return $query;
}
난 당신이 날은이 문제를 해결할 수 있도록 내가 당신에게 충분한 정보를 제공 한 희망 :
다음은 모델 코드입니다.
CakePHP의 버전 :
자세한 내용을 편집하십시오. 코드 전용 및 "시도하십시오"답변은 검색 가능한 콘텐츠가 없으므로 권장하지 않으며 누군가가 "시도해"야하는 이유를 설명하지 않습니다. 우리는 여기서 지식을위한 자원이되기 위해 노력합니다. –