@@ -893,6 +893,15 @@ public function __serialize(): array
893893 unset($ vars ['_data ' ][$ k ]);
894894 } else {
895895 switch ($ this ->_table ->getTypeOf ($ k )) {
896+ case 'json ' :
897+ $ value = json_encode ($ vars ['_data ' ][$ k ]);
898+ if ($ value === false ) {
899+ throw new Doctrine_Record_Exception (
900+ 'Error encountered encoding value: ' . json_last_error_msg ()
901+ );
902+ }
903+ $ vars ['_data ' ][$ k ] = $ value ;
904+ break ;
896905 case 'gzip ' :
897906 $ vars ['_data ' ][$ k ] = gzcompress ($ vars ['_data ' ][$ k ]);
898907 break ;
@@ -941,6 +950,15 @@ public function __unserialize(array $array): void
941950 case 'gzip ' :
942951 $ this ->_data [$ k ] = gzuncompress ($ this ->_data [$ k ]);
943952 break ;
953+ case 'json ' :
954+ $ value = json_decode ($ this ->_data [$ k ]);
955+ if ($ value === null && json_last_error () !== JSON_ERROR_NONE ) {
956+ throw new Doctrine_Record_Exception (
957+ 'Error encountered decoding Json: ' . json_last_error_msg ()
958+ );
959+ }
960+ $ this ->_data [$ k ] = $ value ;
961+ break ;
944962 case 'enum ' :
945963 $ this ->_data [$ k ] = $ this ->_table ->enumValue ($ k , $ this ->_data [$ k ]);
946964 break ;
@@ -1559,6 +1577,23 @@ protected function _set($fieldName, $value, $load = true)
15591577 $ this ->_values [$ fieldName ] = $ value ;
15601578 } elseif (array_key_exists ($ fieldName , $ this ->_data )) {
15611579 $ type = $ this ->_table ->getTypeOf ($ fieldName );
1580+ if ($ type === 'json ' ) {
1581+ // Doing this so that the stored value on the model, and the value retrieved from the database
1582+ // will match types (mainly so associative arrays are converted to stdClass objects)
1583+ $ value = json_encode ($ value );
1584+ if ($ value === false ) {
1585+ throw new Doctrine_Record_Exception (
1586+ 'Error encountered when encoding $value: ' . json_last_error_msg ()
1587+ );
1588+ }
1589+ $ value = json_decode ($ value );
1590+ if ($ value === null && json_last_error () !== JSON_ERROR_NONE ) {
1591+ throw new Doctrine_Record_Exception (
1592+ 'Error encountered decoding Json: ' . json_last_error_msg ()
1593+ );
1594+ }
1595+ }
1596+
15621597 if ($ value instanceof Doctrine_Record) {
15631598 $ id = $ value ->getIncremented ();
15641599
@@ -1938,6 +1973,15 @@ public function getPrepared(array $array = array())
19381973 case 'object ' :
19391974 $ a [$ field ] = serialize ($ this ->_data [$ field ]);
19401975 break ;
1976+ case 'json ' :
1977+ $ value = json_encode ($ this ->_data [$ field ]);
1978+ if ($ value === false ) {
1979+ throw new Doctrine_Record_Exception (
1980+ 'Error encountered when encoding $value: ' . json_last_error_msg ()
1981+ );
1982+ }
1983+ $ a [$ field ] = $ value ;
1984+ break ;
19411985 case 'gzip ' :
19421986 $ a [$ field ] = gzcompress ($ this ->_data [$ field ], 5 );
19431987 break ;
0 commit comments