沃梦达 / 编程问答 / php问题 / 正文

如何通过 Slim php 和 Paris 将主干模型数据发布到数据库

How to POST backbone model data to DB through Slim php and Paris(如何通过 Slim php 和 Paris 将主干模型数据发布到数据库)

本文介绍了如何通过 Slim php 和 Paris 将主干模型数据发布到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 Backbone.js、Slim PHP 和 Paris/Idiorm 可能会一起工作,但我无法完成流程,从模型属性数据开始,一直到数据库.问题:当我执行 model.save() 时,到底什么会被发送到我的服务器?

I'm trying to get an understanding of how Backbone.js, Slim PHP and Paris/Idiorm might work together and I'm having trouble completing the flow, starting with model attribute data, all the way to the database. PROBLEM: What exactly gets sent to my server when I do model.save() ?

客户端:Backbone.js

Client-side: Backbone.js

var Donut = Backbone.Model.extend({
    defaults: {
        name: null,
        sparkles: false,
        creamFilled: false
    },
    url: function() {
        return '/donut';
    }
});

var bostonCream = new Donut({
    name: 'Bawston Cream',
    sparkles: true,
    creamFilled: true
});

bostonCreme.save();  // <-- Problem: Not sure what & format this is sending

我认为以上是我的主要问题.我的理解是,骨干默认情况下会知道发送 POST 数据,因为它是新的.它将它发送到路由的/donut,但我的问题是它发送了什么?以什么格式?我想要的结果是将这些甜甜圈属性保存到我的数据库中.我可以使用 jQuery $.post()...

I think the above is my main problem. My understanding is that backbone will by default, know to send POST data since it's new. It sends it to /donut which is routed, but the question I have is WHAT does it send? And in what format? The outcome I want is to save those donut attributes to my DB. I can pass this server-side code a json like this using jQuery $.post()...

var myDonut = {"name":"Jelly Filled", "sparkles":false, "creamFilled":true};
$.post('http://localhost/donut', myDonut);

...它高兴地接受了它,将它保存到我的数据库中.但是在当前设置尝试发送我的主干甜甜圈数据时,我收到 POST 500 内部服务器错误.下面我有一些服务器端代码.

...and it happily takes it, saves it to my database. But with the current setup trying to send my backbone donut data, I get POST 500 Internal Server Error. Below I have some server-side code.

服务器端:Slim PHP w/Paris

Server-side: Slim PHP w/ Paris

class Donut extends Model {}

$app->post('/donut', function() use ($app) {  // Slim framework routes my POST...

    $donuts = Model::factory('Donut')->create();  // Paris stuff...

    $donuts->name = $app->request()->post('name');  // Slim request parameters...
    $donuts->sparkles = $app->request()->post('sparkles');
    $donuts->creamFilled = $app->request()->post('creamFilled');

    $donuts->save();   // Paris... Save name, sparkles, and creamFilled to my DB
});

我感觉答案就在那里,但我看过的每个例子似乎都遗漏了一个或另一个拼图,我无法理解啊哈!"片刻.如果这是一个非常无知的问题,我提前感谢您并道歉.:-P

I have a feeling the answer is out there, but every example I've looked at seems to be missing one piece of the puzzle or another and I can't get that "A-hA!" moment. I thank you in advance and apologize if this is a really ignorant question. :-P

跟进/1

你能粘贴错误信息吗?

我在当前收到一个 POST http://localhost:8888/donut 500(内部服务器错误)状态.我可以通过以下代码获取更多信息.

I get a POST http://localhost:8888/donut 500 (Internal Server Error) in the current state. I can get more information with the following code.

bostonCream.save({}, {  // REPLACE bostonCream.save();
    success: function(model, response) {
        console.log('SUCCESS:');
        console.log(response);
    },
    error: function(model, response) {
        console.log('FAIL:');
        console.log(response);
    }
});

现在,当我运行主干的 save() 时,我仍然收到 500 错误,但还有 XMLHttpRequest 作为我的 FAIL 响应.来自 XMLHttpRequest 的唯一显着线索是 responseText = SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null.

Now when I run backbone's save(), I still get the 500 Error but also XMLHttpRequest as my FAIL response. The only remarkable clue from the XMLHttpRequest is responseText = SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null.

所以我的猜测是 1) 我在使用 save() 搞砸了一些东西,因为它没有正确捕获我的属性,2) 它当前正在以我的服务器没有的格式发送我的属性使用标准的 $app->request()->post() Slim 方法识别(当我尝试直接使用 $_POST 访问时似乎也没有做太多事情),3)我的服务器没有正确设置来接受这种方法正在发送的数据.

So my guess is that either 1) I'm messing something up with the save() in that it isn't capturing my attributes correctly, 2) It is currently sending my attributes in a format that my server isn't recognizing with the standard $app->request()->post() Slim methods (Doesn't seem to do much when I try accessing directly with $_POST either), 3) My server isn't setup correctly to take the kind of data that is being sent.

我注意到的另一件事是,当我添加时

Another thing I noticed although I don't know what to make of it is that when I add

echo $_POST;

它返回给我一个空数组.仍然给我 FAIL.但是,如果我这样做...

It returns to me an empty array. Still gives me the FAIL. If I do THIS however...

echo json_encode($_POST);

它给了我一个 SUCCESS 并且响应是 [ ].里面什么都没有.显然,我的 POST 数据仍然不稳定.

It gives me a SUCCESS and the response is a [ ]. Nothing in there. Clearly my POST data is still wonky.

推荐答案

我想出了一个解决方案来完成这个问题:如何使用默认的主干 save() 和 .sync 从客户端获取数据到服务器 - 传递给Slim php 框架并通过 Paris/Idiorm 进入我的数据库.

I came up with a solution to completing the problem: how to get data from client to server using the default backbone save() and .sync - passed over to the Slim php framework and going through Paris/Idiorm to my DB.

我在下面包含了我的工作更新代码:

I am including my working updated code below:

客户端:Backbone.js

var Donut = Backbone.Model.extend({
    defaults: {
        name: null,
        sparkles: false,
        creamFilled: false
    },
    url: function() {
        return '/donut';
    }
});

var bostonCream = new Donut({
    name: 'Bawston Cream',
    sparkles: true,
    creamFilled: true
});

bostonCream.save();

/***** If you want to check out the response to save() ? ***
bostonCream.save({}, {
    success: function(model, response) {
        console.log('SUCCESS:');
        console.log(response);
    },
    error: function(model, response) {
        console.log('FAIL:');
        console.log(response);
    }
});
************************************************************/

服务器端:Slim PHP w/Paris/Idorm

class Donut extends Model {}

$app->post('/donut', function() use ($app) {

    $donuts = Model::factory('Donut')->create();

    /* EDIT: Works... but not the Slim way
    $parameters = json_decode(file_get_contents('php://input'), true);
    $donuts->name = $parameters['name'];
    $donuts->sparkles = $parameters['sparkles'];
    $donuts->creamFilled = $parameters['creamFilled']; */

    /* SLIM: Using Slim Request Object */
    $requestBody = $app->request()->getBody();  // <- getBody() of http request
    $json_a = json_decode($requestBody, true);
    $donuts->name = $json_a['name'];
    $donuts->sparkles = $json_a['sparkles'];
    $donuts->creamFilled = $json_a['creamFilled'];

    $donuts->save();

    // echo json_encode($parameters); // Prove you've captured POST data, send it back
}

现在我的代码很高兴地使用 Backbone.js 的默认设置(没有更改同步)并将正确的模型属性信息发送到我的服务器,这似乎成功地接受了数据并将其保存到我的数据库中.

Now my code is happily using the default settings of Backbone.js (no changes to sync) and sending proper model attribute information to my server which seems to be successfully accepting the data and saving it to my DB.

这里的关键似乎是这一行...

The key here seems to be this line...

/* $parameters = json_decode(file_get_contents('php://input'), true); */
// EDITED: getBody() method not documented in Develop Doc, only Stable @ time of post

$requestBody = $app->request()->getBody();

这篇关于如何通过 Slim php 和 Paris 将主干模型数据发布到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何通过 Slim php 和 Paris 将主干模型数据发布到数据库

基础教程推荐