Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 14th, 2012  |  syntax: None  |  size: 0.91 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get actual integers from MySQL with CakePHP
  2. ===========================================
  3.  
  4. With this AppModel, any integer column from the database will be casted to a proper int, any time you read it from the database.
  5.  
  6. Why would I want that?
  7. ----------------------
  8.  
  9. Because it's awesome to send data to the client as JSON.
  10.  
  11. `json_encode()` does a great job of encoding your associative arrays in PHP to JSON. But where Javascript (and hence JSON) is picky about types, PHP is more lax. That means you might end up with JSON full of "1234" instead of 1234.
  12.  
  13. On the serverside, it won't matter, most of the time, since PHP silently cast strings to integers for you. It becomes slightly more annoying when you actually want to use this data on the client, since you will probably need to convert your numeric strings to actual integers manually.
  14.  
  15. -- "But won't this be awfully slow?", I hear you asking.
  16.  
  17. You use PHP. You don't care.