내 RESTFul GET, DELETE가 작동하지만 이상하게도 PUT (편집)가 데이터베이스를 업데이트하지 못했습니다.
포스트 편집을위한 핸들 바 템플릿 :
<script type="text/x-handlebars" id="post">
<h1>View/Update Posts</h1>
{{#if isEditing}}
<p>Title: {{input type="text" value=title}}</p>
<p>Author: {{input type="text" value=author}}</p>
<p>Body: {{textarea value=body}}</p>
<button {{action 'doneEditing' this}}>Done</button>
{{else}}
<p>Title : {{title}}</p>
<p>Author: {{author}}</p>
<p>Body : {{body}}</p>
<button {{action 'edit'}}>Edit</button>
{{/if}}
PostController
App.PostController = Ember.ObjectController.extend({
isEditing: false,
actions: {
edit: function() {
this.set('isEditing', true);
},
doneEditing: function(post) {
this.set('isEditing', false);
post.save(); //NOT WORKING - NOT UPDATING THE DATABASE RECORD!!!
}
}
});
REST 어댑터 및 데이터 모델
App.Post = DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
body: DS.attr('string')
});
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'emberpostsrest/api'
});
휴먼 앱 URL
http://localhost/emberpostsrest
RESTFul 서버 (SLIM PHP 사용)
http://localhost/emberpostsrest/api
작업 REST
http://localhost/emberpostsrest/api/posts (GET all)
http://localhost/emberpostsrest/api/posts/1 (GET via id)
http://localhost/emberpostsrest/api/posts/1 (DELETE via id)
편집 용 RESTFul API는 이미 PHP curl을 사용하여 테스트되었으며 정상적으로 작동합니다.
//PUT - update
$data = array("id" => 3, "title" => "3", "author" => "2", "body" => "1");
$data_string = json_encode($data);
$ch = curl_init('http://localhost:8080/emberpostsrest/api/posts/3');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
슬림 한 PHP 코드
$app->put('/posts/:id', 'updatePostByID'); //update post via id
function updatePostByID($id)
{
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
$post = json_decode($body);
$sql = "UPDATE posts
SET title = :title,
author = :author,
body = :body
WHERE id = :id";
try
{
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $post->id);
$stmt->bindParam("title", $post->title);
$stmt->bindParam("author", $post->author);
$stmt->bindParam("body", $post->body);
$stmt->execute();
}
catch(PDOException $e)
{
$errorMessage = $e->getMessage();
}
}
도움을 주셔서 감사합니다 : D
Ember에서 들어오는 데이터를 검사하면 값이 "posts"네임 스페이스에 입력되어 있음을 알 수 있습니다.
즉,이 대신 :
{
"id": "1",
"title": "my title",
"author": "me",
"body": "the body"
}
다음과 같이 들어옵니다.
{
"posts": {
"id": "1",
"title": "my title",
"author": "me",
"body": "the body"
}
}
이것은 ember-data에 대한 규칙이므로 Ember 측에서 문제를 해결하기보다는 PHP 코드를 업데이트하고 싶을 것입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다