Hateoas Публикует,Помещает,Удаляет PHP, Codeigniter

#php #json #codeigniter #hateoas

Вопрос:

может ли кто-нибудь привести пример того, как кодировать POST, PUT, Удалять Hateoas PHP в Codeigniter, у меня есть 2 таблицы, автор(AuthorID-первичный ключ, BookID-внешний ключ), книга(BookID-первичный ключ) У меня есть Index_get

 public function index_get(){  $id = $this-gt;get('AuthorID');  $author =[];  if ($id ==""){  $data = $this-gt;db-gt;get("author")-gt;result();  foreach($data as $row=gt;$key):  $author[]=["AuthorID"=gt;$key-gt;AuthorID,  "AuthorName"=gt;$key-gt;AuthorName,  "_link"=gt;[(object)["href"=gt;"book/{$key-gt;BookID}",  "rel"=gt;"book",  "type"=gt;"GET"]],  "BirthPlace"=gt;$key-gt;BirthPlace,  "Email"=gt;$key-gt;Email,  "Education"=gt;$key-gt;Education,  "Phone"=gt;$key-gt;Phone,  "Address"=gt;$key-gt;Address,  "Nationality"=gt;$key-gt;Nationality  ]; endforeach;  }  else {  $this-gt;db-gt;where("AuthorID","$id");  $data = $this-gt;db-gt;get("author")-gt;result();  }  $result = ["took"=gt;$_SERVER["REQUEST_TIME_FLOAT"], "code"=gt;200, "message"=gt;"Show Data Success", "author"=gt;$author]; $this-gt;response($result, 200); }  

и это успех

 "took": 1637045427.669679, "code": 200, "message": "Show Data Success", "author": [  {  "AuthorID": "A1",  "AuthorName": "Jack Dane",  "_link": [  {  "href": "book/1",  "rel": "book",  "type": "GET"  }  ],  "BirthPlace": "London",  "Email": "Jackdane@gmail.com",  "Education": "Bachelor At Univercity Of Canada",  "Phone": "09199191",  "Address": "4457 Grey Fox Farm Road, Houston, Texas",  "Nationality": "America"  },  {  "AuthorID": "A2",  "AuthorName": "Michael Dendy",  "_link": [  {  "href": "book/2",  "rel": "book",  "type": "GET"  }  ],  "BirthPlace": "New York",  "Email": "Michaeldendy@gmail.com",  "Education": "Bachelor At Univercity Of California",  "Phone": "09188181",  "Address": "2602 Farnum Road, New York, New York",  "Nationality": "America"  },  {  "AuthorID": "A3",  "AuthorName": "Dumando",  "_link": [  {  "href": "book/3",  "rel": "book",  "type": "GET"  }  ],  "BirthPlace": "Jakarta",  "Email": "Dumando@gmail.com",  "Education": "Bachelor At Indonesia Univercity",  "Phone": "012929919",  "Address": "Jl Cendrawaih 919 Jakarta",  "Nationality": "Indonesia"  },  {  "AuthorID": "A4",  "AuthorName": "dono",  "_link": [  {  "href": "book/4",  "rel": "book",  "type": "GET"  }  ],  "BirthPlace": "Beijing",  "Email": "dono@gmail.com",  "Education": "Bachelor at ITS",  "Phone": "0919191",  "Address": "Jl. MTHaryono",  "Nationality": "china"  } ] }  

Но когда я пытаюсь отправить сообщение в postman, он выдает мне ошибку «Столбец ‘AuthorID’ не может быть пустым». AuthorID-это первичный ключ.это дает мне ошибку «Столбец ‘AuthorID’ не может быть пустым», но у AuthorID уже есть входные данные, я просто хочу изменить место рождения.

 function index_post() {  $author = array(  'AuthorID' =gt; $this-gt;post('AuthorID'),  'AuthorName' =gt; $this-gt;post('AuthorName'),  'BookID' =gt; $this-gt;post('BookID'),  'BirthPlace' =gt; $this-gt;post('BirthPlace'),  'Email' =gt; $this-gt;post('Email'),  'Education' =gt; $this-gt;post('Education'),  'Phone' =gt; $this-gt;post('Phone'),  'Address' =gt; $this-gt;post('Address'),  'Nationality' =gt; $this-gt;post('Nationality'));   $insert = $this-gt;db-gt;insert('author', $author);  if ($insert) {  $result = ["took"=gt;$_SERVER["REQUEST_TIME_FLOAT"],  "code"=gt;200,  "message"=gt;"Add Data Success",  "author"=gt;$author];  $this-gt;response($result, 200);  } else {  $result = ["took"=gt;$_SERVER["REQUEST_TIME_FLOAT"],  "code"=gt;200,  "message"=gt;"Failed Add Data",  "author"=gt;$author];  $this-gt;response($result, 502);  } }  

when I try PUT in postman it give me success message but the data is not update. AuthorID is null, BoodID is null, i already input AuthorID, BookID in postman and still data cannot update,

 function index_put() {  $id = $this-gt;put('AuthorID');  $author = array(  'AuthorID' =gt; $this-gt;put('AuthorID'),  'AuthorName' =gt; $this-gt;put('AuthorName'),  'BookID' =gt; $this-gt;put('BookID'),  'BirthPlace' =gt; $this-gt;put('BirthPlace'),  'Email' =gt; $this-gt;put('Email'),  'Education' =gt; $this-gt;put('Education'),  'Phone' =gt; $this-gt;put('Phone'),  'Address' =gt; $this-gt;put('Address'),  'Nationality' =gt; $this-gt;put('Nationality'));  $this-gt;db-gt;where('AuthorID', $id);  $update = $this-gt;db-gt;update('author', $author);  if ($update) {  $result = ["took"=gt;$_SERVER["REQUEST_TIME_FLOAT"],  "code"=gt;200,  "message"=gt;"Data Update Success",  "author"=gt;$author];  $this-gt;response($result, 200);  } else {  $result = ["took"=gt;$_SERVER["REQUEST_TIME_FLOAT"],  "code"=gt;200,  "message"=gt;"Failed Update Data",  "author"=gt;$author];  $this-gt;response($result, 502);  } }  

результат index_put

 { "took": 1637045848.425828, "code": 200, "message": "Data Update Success", "author": {  "AuthorID": null,  "AuthorName": "Jack Dane",  "BookID": null,  "BirthPlace": "Jakarta",  "Email": "Jackdane@gmail.com",  "Education": "Bachelor At Univercity Of Canada",  "Phone": "09199191",  "Address": "4457 Grey Fox Farm Road, Houston, Texas",  "Nationality": "America" }  

}

и когда я пытаюсь УДАЛИТЬ, он дает мне «статус»: «Успешно удалить данные», но данные не удаляются, когда я регистрируюсь в phpMyAdmin.

 function index_delete() {  $id = $this-gt;delete('AuthorID');  $this-gt;db-gt;where('AuthorID', $id);  $delete = $this-gt;db-gt;delete('author');  if ($delete) {  $this-gt;response(array('status' =gt; 'Success Delete Data'), 200);  }     else{  $this-gt;response(array('status' =gt; 'fail Delete Data', 502));  } }  

Комментарии:

1. Я думаю, вам нужно проверить null значения вручную. Если такое значение получено, не добавляйте соответствующий ключ в массив значений, подлежащих обновлению

2. для index_put правильно?, я удаляю AuthorID,BookID в массиве, значение действительно получено в результате json, но база данных не обновляется, поэтому, когда я использую GET. значение не обновлялось. вы можете помочь?

3. «База данных не обновляется» — что это значит? Что вы пытались проверить, почему это не работает?

4. я имею в виду, что когда я проверяю таблицу в phpmyadmin, значение не изменилось. например, когда я использую put в postman, я меняю имя автора»джек» на имя автора»денис», массив в json меняется, но в phpmyadmin не изменился