Jump to content

Recommended Posts

Hi,

 

I use PDO MySQL with Zend Framework database adapters. When I select data from the database, it is always returned as strings. From what I have understood, this is the normal behavior. But this creates a lot of unnecessary typecasting code in various places of the application. One example is when data is encoded into JSON and used in javascripts. Assuming that everything are strings won't work since some variables might have types if they were not pulled from the database.

 

Is it possible to have some kind of automatic type hinting for database adapters so that all data comes in the right types from the database, or are there any other good solutions to this problem?

 

Thank you for your help!

 

-F

 

 

Has anyone else this problem ever or something similar when getting data from the database and JSON-encoding it for some ajax requests. Or is it just something you have to live with (either you have to typecast the data in the php-code or you have to do it javascript (possibly with some additional checks).

The problem is that the variables may come as either strings or numbers. Especially booleans. Check this json string for example:

 

{

  "booleanFromDB":"true",

  "booleanFromPHP":true,

  "integerFromDB":"123",

  "integerFromPHP":123

}

 

I would like to avoid all such code on the js as:

 

if (someVariable == "true" || someVariable == true) { do something ... }

 

I would like to find a somewhat generic way to send data from MySQL->PHP->JS and back (JS->PHP->MySQL) without having to do lot's of typecasting or checks.

 

Thanks,

F

 

Assuming that you have fetched a row from the database in $row, the following will handle the conversion of numeric values -

 

<?php
function make_int($val){
return is_numeric($val) ? (int)$val : $val ;
}

$row = array_map('make_int',$row);

echo json_encode($row);
?>

 

The above could be extended to handle boolean values.

The following works for float, integer, string, and boolean -

 

<?php

function convert_type($val){
if(is_numeric($val)){
	return $val + 0;
}
if(strtolower($val) == 'false'){
	return false;
}
if(strtolower($val) == 'true'){
	return true;
}	
return $val;
}

$row = array_map('convert_type',$row);

echo json_encode($row);
?>

Thank you for your answer!

 

Does this have a risk that some real strings get casted to boolean or int? For example if we have a field username and someone writes "true" or "five" or something like that?

 

One option that I have been thinking about is to write a short script that can be run from the commandline, which would generate classes from the database scheema that take care of the typecasting for each table. Do you know if there is something like this already for Zend Framework?

 

-F

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.