如何通过超薄的PHP和巴黎POST骨干模型数据到数据库 [英] How to POST backbone model data to DB through Slim php and Paris

查看:195
本文介绍了如何通过超薄的PHP和巴黎POST骨干模型数据到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让如何 Backbone.js的,的超薄的PHP 巴黎/ 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数据,因为它是新的。它把它发送到/这是路由甜甜圈,但我的问题是,是什么派?在什么格式?我想要的结果是那些甜甜圈属性保存到我的数据库。我可以通过这个服务器端code像这样使用jQuery .post的$)一个JSON(...

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内部服务器错误。下面我有一些服务器端的code。

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

服务器端:修身PHP瓦特/巴黎

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

我有一种感觉,答案就​​在那里,但每次我看着例子似乎缺少一块拼图或其他的,我不能得到A-HA!时刻。我感谢你提前道歉,如果这是一个非常无知的问题。 :-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:在当前状态下8888 /甜甜圈 500(内部服务器错误)://本地主机。我可以用下面的code的更多信息。

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错误,而且XMLHtt prequest为我的失败响应。从XMLHtt prequest唯一显着的线索是responseText的= SQLSTATE [23000]:完整性约束冲突:1048列'名称'不能为空

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)我的东西搞​​乱与保存(),它是不正确捕获我的属性,2)当前正在发送我的属性,在我的服务器是不是格式用)标准$ APP->请求(确认 - >后()方法超薄(似乎并不当我尝试直接与$ _ 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;

它返回给我一个空数组。仍然给我的失败。如果我这样做不过...

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

echo json_encode($_POST);

这给了我成功,响应是[]。没有在那里。显然,我的POST数据仍是靠不住的。

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

推荐答案

我想出了一个解决方案,完成了问题:如何使用默认骨干保存从客户端获得数据到服务器()和.SYNC - 传递到修身PHP框架,并通过巴黎/ 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.

我在下面,包括我的工作更新code:

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);
    }
});
************************************************************/

Sever的边:超薄的PHP瓦特/巴黎/ 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
}

现在我的code是愉快地使用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();

这篇关于如何通过超薄的PHP和巴黎POST骨干模型数据到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆