title: How to POST backbone model data to DB through Slim php and Paris
body: <p>I'm trying to get an understanding of how <a href="http://documentcloud.github.com/backbone/" rel="noreferrer">Backbone.js</a>, <a href="http://www.slimframework.com/" rel="noreferrer">Slim PHP</a> and <a href="http://j4mie.github.com/idiormandparis/" rel="noreferrer">Paris/Idiorm</a> 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() ?</p>
  
  <p>Client-side: Backbone.js </p>
  
  <pre><code>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();  // &lt;-- Problem: Not sure what &amp; format this is sending
  </code></pre>
  
  <p>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()...</p>
  
  <pre><code>var myDonut = {"name":"Jelly Filled", "sparkles":false, "creamFilled":true};
  $.post('http://localhost/donut', myDonut);
  </code></pre>
  
  <p>...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. </p>
  
  <p>Server-side: Slim PHP w/ Paris</p>
  
  <pre><code>class Donut extends Model {}
  
  $app-&gt;post('/donut', function() use ($app) {  // Slim framework routes my POST...
  
      $donuts = Model::factory('Donut')-&gt;create();  // Paris stuff...
  
      $donuts-&gt;name = $app-&gt;request()-&gt;post('name');  // Slim request parameters...
      $donuts-&gt;sparkles = $app-&gt;request()-&gt;post('sparkles');
      $donuts-&gt;creamFilled = $app-&gt;request()-&gt;post('creamFilled');
  
      $donuts-&gt;save();   // Paris... Save name, sparkles, and creamFilled to my DB
  });
  </code></pre>
  
  <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</p>
  
  <p><strong>FOLLOWUP/EDIT: 1</strong></p>
  
  <blockquote>
    <p>Can you paste the error messages?</p>
  </blockquote>
  
  <p>I get a POST <a href="http://localhost:8888/donut" rel="noreferrer">http://localhost:8888/donut</a> 500 (Internal Server Error) in the current state. I can get more information with the following code.</p>
  
  <pre><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);
      }
  });
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Another thing I noticed although I don't know what to make of it is that when I add</p>
  
  <pre><code>echo $_POST;
  </code></pre>
  
  <p>It returns to me an empty array. Still gives me the FAIL. If I do THIS however...</p>
  
  <pre><code>echo json_encode($_POST);
  </code></pre>
  
  <p>It gives me a SUCCESS and the response is a [ ]. Nothing in there. Clearly my POST data is still wonky. </p>