2014-08-27 37 views
1

我的REST風格的獲取,刪除工作的PUT(編輯)奇怪的是未能更新數據庫:Ember.js REST更新(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 

基於REST(採用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) 

爲編輯REST風格的API使用PHP捲曲已經測試服務器工作正常:

//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

回答

2

我懷疑,如果你檢查從灰燼傳入的數據,你會看到值中楔入式「職位」命名空間。

換句話說,而不是這樣的:

{ 
    "id": "1", 
    "title": "my title", 
    "author": "me", 
    "body": "the body" 
} 

它的到來,因爲這樣有:

{ 
    "posts": { 
    "id": "1", 
    "title": "my title", 
    "author": "me", 
    "body": "the body" 
    } 
} 

這是燼數據的慣例,所以你可能會想升級你的PHP代碼,而不是試圖在Ember方面解決它。

+0

確實極難檢查提交給PH的json結構P.任何建議? – 2014-08-27 12:03:21

+0

我不是PHP開發人員,所以我不確定。你可以將它註銷到控制檯或在請求進入時使用調試器嗎? – 2014-08-27 12:15:05

+0

我在我的REST函數中創建了一個日誌編寫器,這是數據:{「post」:{「title」:「Ember是最好的」,「作者」:「rbk」,「body」:「SPA Rock」} }。沒有ID大聲笑 – 2014-08-27 12:48:10

1

我按照@Beerlington的建議解決了代碼,我將從Ember提交的json記錄到我的PHP Rest函數中。這是json對象

{ 
    "post": { 
    "title": "my title", 
    "author": "me", 
    "body": "the body" 
    } 
} 

如果你注意到,沒有ID,它阻止更新因爲一個是主鍵lol。所以我更新我的Ember模型。

App.Post = DS.Model.extend({ 
    postId: DS.attr('string'), 
    title: DS.attr('string'), 
    author: DS.attr('string'), 
    body: DS.attr('string') 
}); 

通知帖子ID沒有標識 - 灰燼數據不會允許ID作爲模特屬性

編輯控制器

doneEditing: function(post) { 
    post.set("postId", post.id); 
    post.save() 
    this.set('isEditing', false); 
} 

最後提交的更新我的PHP休息函數來處理JSON結構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(); 
    } 
}