Reputation: 976
I have a Zend Form that allows you to add a college class to the database. I collect the data and persist it with Doctrine 2. Everything is fine the data is in the table. When I retrieve the data everything is ready to use.
array
0 => &
array
'id' => int 151
'className' => string 'Geocaching (Jee-oh-Cash-ing) is part of a worldwide outdoor game for GPS users. Go on an adventure to find hidden treasure, called “geocaches”. If you own a GPS receiver or a smartphone, bring it (preferred, but not required) along with some fresh batteri' (length=255)
'instructor' => string 'Geocaching (Jee-oh-Cash-ing) is part of a worldwide outdoor game for GPS users. Go on an adventure to find hidden treasure, called “geocaches”. If you own a GPS receiver or a smartphone, bring it (preferred, but not required) along with some fresh batteri' (length=255)
'classDescription' => string 'Geocaching (Jee-oh-Cash-ing) is part of a worldwide outdoor game for GPS users. Go on an adventure to find hidden treasure, called “geocaches”. If you own a GPS receiver or a smartphone, bring it (preferred, but not required) along with some fresh batteri' (length=255)
Then I am using Jquery DataTables to display all the table data. I have a view helper that renders the jquery for the datatable. Inside the view helper I am using
Zend_Json::encode(array_merge($this->_defaultOptions, $options), false, array('enableJsonExprFinder' => true));
All the values that have a double quote get encoded as null.
"aaData":{"id":151,"className":null,"instructor":null,"classDescription":null,}}'
Any other values will display in the DataTable except any value that has a double quote.
I must being doing something really wrong because I also have this trouble when I try to re-populate the Zend Form with the data to do an update.
$results = $this->_doctrine->getEntityManager()->getRepository('My\Entity')->findOneBy($request->getParam('id'));
$form->setDefaults($results[0]);
Again if I dump the results from Doctrine all the quoted data is there ready to be used. But after $form->setDefaults($results[0]) the fields in the form are blank.
Any help is really appreciated.
Upvotes: 0
Views: 2180
Reputation: 43760
I had the same problem. The solution is that the quotes were not the " but rather “ (the Microsoft encoded quotes) which was causing json_encode()
to return null. Doing a replace with the method from this answer (How to replace Microsoft-encoded quotes in PHP) fixed it.
UPDATE:
Zend also has an encoder that parses the string for you. But you need to set Zend_Json::$userBuiltinEncoderDecoder = true
in your bootstrap and it will then use it instead of php's json_encode
Upvotes: 2
Reputation: 8519
I think you need to use the constant JSON_HEX_QUOT
this seems to work:
$options = array(JSON_HEX_QUOT);
$json = Zend_JSON($value, $cyclecheck, $options);
I got deeper into the Zend/Json.php code and it looks like if you'd like to use JSON_HEX_QUOT you're going to have to use the PHP function as Zend_Json doesn't pass the constant.
// Encoding
if (function_exists('json_encode') && self::$useBuiltinEncoderDecoder !== true) {
$encodedResult = json_encode($valueToEncode);
I think this is because ZF is coded to the PHP 5.2.6 standard and $options was added to json_encode in PHP 5.3.0
here is the reference from the php manual:
Example #2 *A json_encode() example showing all the options in action*
<?php $a = array('<foo>',"'bar'",'"baz"','&blong&');
echo "Normal: ", json_encode($a), "\n"; echo "Tags: ",
json_encode($a,JSON_HEX_TAG), "\n"; echo "Apos: ",
json_encode($a,JSON_HEX_APOS), "\n"; echo "Quot: ",
json_encode($a,JSON_HEX_QUOT), "\n"; echo "Amp: ",
json_encode($a,JSON_HEX_AMP), "\n"; echo "All: ",
json_encode($a,JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP), "\n\n";$b = array();
echo "Empty array output as array: ", json_encode($b), "\n"; echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";
$c = array(array(1,2,3));
echo "Non-associative array output as array: ", json_encode($c), "\n"; echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";
$d = array('foo' => 'bar', 'baz' => 'long');
echo "Associative array always output as object: ", json_encode($d), "\n"; echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
Upvotes: 0