gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:07:07 26 4
gpt4 key购买 nike

我正在尝试了解 Backbone.js 是如何实现的, Slim PHPParis/Idiorm可能一起工作,但我在完成流程时遇到了麻烦,从模型属性数据开始,一直到数据库。问题:当我执行 model.save() 时,究竟发送到我的服务器的是什么?

客户端: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

我认为以上是我的主要问题。我的理解是 Backbone 将默认知道发送 POST 数据,因为它是新的。它将它发送到路由的/donut,但我的问题是它发送了什么?以什么格式?我想要的结果是将这些 donut 属性保存到我的数据库中。我可以使用 jQuery $.post()...将此服务器端代码传递给这样的 json...

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

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

服务器端: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
});

我觉得答案就在那里,但我看过的每个例子似乎都缺少一 block 或另一 block 拼图,我无法得到“啊哈!”片刻。我提前感谢你,如果这是一个非常无知的问题,我深表歉意。 :-P

跟进/编辑:1

Can you paste the error messages?

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

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

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

所以我的猜测是 1) 我把 save() 弄乱了,因为它没有正确捕获我的属性,2) 它目前正在以我的服务器不支持的格式发送我的属性识别标准的 $app->request()->post() Slim 方法(当我尝试直接使用 $_POST 访问时似乎没有做太多),3)我的服务器没有正确设置以接受这种正在发送的数据。

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

echo $_POST;

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

echo json_encode($_POST);

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

最佳答案

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

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

客户端: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 的默认设置(同步没有更改)并向我的服务器发送正确的模型属性信息,这似乎成功地接受了数据并将其保存到我的数据库中。

这里的关键似乎是这一行……​​

/* $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 - 如何通过 Slim php 和 Paris 将 Backbone 模型数据发布到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9053405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com