Ember.js REST更新(PUT)不工作(Ember.js REST update (PUT)

2019-10-20 19:30发布

我的REST的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

REST风格的服务器(使用PHP SLIM)

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

Answer 1:

我怀疑,如果你检查从灰烬传入的数据,你会看到值在“上岗”命名空间中键入。

换句话说,而不是这样的:

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

它的到来,因为这样有:

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

这是烬数据的惯例,所以你可能会想升级你的PHP代码,而不是试图解决它的灰烬侧。



Answer 2:

我的解决代码由@Beerlington的建议,我记录从灰烬提交给我的PHP REST功能的JSON。 这是JSON对象

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

如果您发现,没有ID,其中防止更新表兄弟姐妹,一个是主键笑。 所以,我更新我的灰烬模型。

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结构

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();
    }
}


文章来源: Ember.js REST update (PUT) not working