0
我想獲得分頁和JSON響應顯示之前修改對象,現在我的代碼如下所示:訪問每個對象和分頁添加信息[CakePHP的3]
$this->paginate = [
'conditions' => $this->getPaginationConditions(),
];
$this->set('reservations', $this->paginate($this->Reservations));
$this->set('_serialize', ['reservations']);
getPaginationConditions()的外觀這樣
private function getPaginationConditions()
{
$conditions = ['Reservations.id_venue' => $this->Auth->user('id_venue_manager')];
if ($date = $this->request->query('date')) {
$conditions['Reservations.date'] = $date;
}
if ($user = $this->request->query('id_user')) {
$conditions['Reservations.id_user'] = $user;
}
return $conditions;
}
這將返回:
{
"reservations": [
{
"id_reservation": 139,
"reservation_type": "",
"date": "2017-09-04T00:00:00",
"time": "2017-03-01T10:00:00",
"id_venue": 1,
"id_user": 149,
},
{
"id_reservation": 140,
"reservation_type": "",
"date": "2017-09-04T00:00:00",
"time": "2017-03-01T20:00:00",
"id_venue": 1,
"id_user": 149,
},
[...]
我要訪問的每個對象遵循我在分頁之前的條件並添加一個「用戶」字段,我知道如何找到我的用戶,但我不知道如何訪問將通過分頁條件選擇的對象。它看起來像:
{
"reservations": [
{
"id_reservation": 139,
"reservation_type": "",
"date": "2017-09-04T00:00:00",
"time": "2017-03-01T10:00:00",
"id_venue": 1,
"id_user": 149,
"user" : {
"firstname" : "test",
"lastname" : "test",
"id_user" : "149",
[...]
}
},
{
"id_reservation": 140,
"reservation_type": "",
"date": "2017-09-04T00:00:00",
"time": "2017-03-01T20:00:00",
"id_venue": 1,
"id_user": 149,
"user" : {
"firstname" : "test",
"lastname" : "test",
"id_user" : "149",
[...]
}
},
[...]
感謝您的幫助!
看來你沒有關注的CakePHP [命名轉換(https://book.cakephp.org/3.0/en/intro/conventions.html),你應該遵循CakePHP的命名轉換。這將有助於提高生產力 – tarikul05