Skip to content

Commit

Permalink
handle more different data type castins in _get
Browse files Browse the repository at this point in the history
  • Loading branch information
Hedzer committed Sep 25, 2024
1 parent db948d3 commit f04ffbd
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions lib/Doctrine/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -1357,19 +1357,41 @@ protected function _get($fieldName, $load = true)
}

if ($this->_data[$fieldName] === self::$_null) {
$value = null;
return null; // Quick exit, no need to handle type casting
} else {
$value = $this->_data[$fieldName];
}

if (is_null($value)) {
return null;
}

if ($value instanceof Doctrine_Record) {
$_id = $value->getIncremented();
if (!is_null($_id)) {
$value = $_id;
} else {
return $value; // Quick exit, type casting breaks relation saving, Doctrine needs it to be the object
}
}
// Here we support typecasting, so we always return data in the format of the column
$type = $this->_table->getTypeOf($fieldName);
switch($type) {
switch ($type) {
case 'json':
$value = json_decode($value);
$value = json_decode($value);
break;
case 'decimal':
$value = (string)$value;
break;
case 'integer':
$value = (int)$value;
break;
case 'float':
case 'doable':
$value = floatval($value);
break;
}

return $value;
}

Expand Down Expand Up @@ -1469,6 +1491,23 @@ protected function _set($fieldName, $value, $load = true)
}
}

switch($type) {
case 'json':
if (!json_validate($value)) {
$value = json_encode($value);
}
break;
case 'decimal':
$value = (string)$value;
break;
case 'float':
$value = (float)$value;
break;
case 'int':
$value = (int)$value;
break;
}

if ($load) {
$old = $this->get($fieldName, $load);
} else {
Expand Down

0 comments on commit f04ffbd

Please sign in to comment.