问题
My RESTFul GET, DELETE is working however the PUT (edit) strangely enough is fail to update the database:
Handlebar template for Post editing:
<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}}
The 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 adapter and data model
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'
});
Ember App URL
http://localhost/emberpostsrest
RESTFul server (using SLIM PHP)
http://localhost/emberpostsrest/api
Working 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 for edit already tested using PHP curl and is working fine:
//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))
);
Slim PHP code
$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();
}
}
thank you for any help :D
回答1:
I suspect if you inspect the incoming data from Ember, you'll see that the values are keyed within the "posts" namespace.
In other words, instead of this:
{
"id": "1",
"title": "my title",
"author": "me",
"body": "the body"
}
It's coming in as this:
{
"posts": {
"id": "1",
"title": "my title",
"author": "me",
"body": "the body"
}
}
This is the convention for ember-data so you'll probably want to update your PHP code, rather than try to work around it on the Ember side.
回答2:
My solving code as suggested by @Beerlington, I log the json submitted from Ember to my PHP Rest function. This is the json object
{
"post": {
"title": "my title",
"author": "me",
"body": "the body"
}
}
If you notice, there is no ID, which prevent the updating coz that one is primary key lol. So I update my Ember Model.
App.Post = DS.Model.extend({
postId: DS.attr('string'),
title: DS.attr('string'),
author: DS.attr('string'),
body: DS.attr('string')
});
Notice postId not Id - Ember data not allow Id as model properties
The editing controller
doneEditing: function(post) {
post.set("postId", post.id);
post.save()
this.set('isEditing', false);
}
And finally I update my PHP Rest function to handle the json structure as submitted by Ember
function updatePostByID($id)
{
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
$data = json_decode($body);
//logging json data received from Ember!
//$file = 'json.txt';
//file_put_contents($file, json_encode($data));
$post = null;
foreach($data as $key => $value) {
$postData = $value;
}
$post = new Post();
foreach($postData as $key => $value) {
if ($key == "postId")
$post->id = $value;
if ($key == "title")
$post->title = $value;
if ($key == "author")
$post->author = $value;
if ($key == "body")
$post->body = $value;
}
$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();
}
}
来源:https://stackoverflow.com/questions/25521859/ember-js-rest-update-put-not-working