問題描述
我試圖了解 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 將主干模型數據發布到數據庫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!