-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DDC-3863 add a json
type that doesn't have the flaws of json_array
#892
Conversation
Hello, thank you for creating this pull request. I have automatically opened an issue http://www.doctrine-project.org/jira/browse/DBAL-1277 We use Jira to track the state of pull requests and the versions they got |
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the extra empty line should be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
49945bb
to
54f73b5
Compare
return null; | ||
} | ||
|
||
return json_encode($value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we throw an exception here, too if encoding fails?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should throw some variant of ConversionException with the return value of json_last_error().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I do it too for the convertToPHPValue
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No - the logic there is fine - ConversionException is missing a static constructor for this PHP->database scenario - there should probably be a ConversionException::serializationFailed method or something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Taluu the PR seems fine so far but still throwing a ConversionException
if json_encode()
fails is missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I was on it, and I just updated it (and rebased it with the master branch)
{ | ||
if (null === $value) { | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this actually be the case ? shouldn't it be converted to a json null instead ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite sure if it should be encoded too, if the field can be nullable ? Otherwise, it gives a "null"
string when encoded in json...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stof @Taluu Kind of debatable -
php > var_dump(json_decode('null') === json_decode(null));
bool(true)
If the platform has native json support (like postgresql) it will be stored as null. If the platform doesn't have native json support, then storing it as a SQL null will achieve much the same result, as deserializing the value to PHP value will be the same whether it's 'null'
or NULL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, but on json_encode
, it differs ;
Psy Shell v0.6.1 (PHP 7.0.2 — cli) by Justin Hileman
>>> json_encode('null');
=> ""null""
>>> json_encode(null);
=> "null"
And then on a json_decode
call :
Psy Shell v0.6.1 (PHP 7.0.2 — cli) by Justin Hileman
>>> json_decode(null);
=> null
>>> json_decode('null');
=> null
>>> json_decode('"null"');
=> "null"
So I think returning a null value when the value is actually null should be more appropriate (Sorry for the late answer...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to prefer SQL NULL
whenever possible as it seems more convenient than having a JSON null
string stored (might also be more efficient storage wise...). Also it avoids unnecessary json_decode()
calls for converting back to PHP which might improve performance. If nobody disagrees, I'll accept the current implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is also coming to my mind is database queries against JSON columns where you want to filter "empty" (NULL) values which would not be possible via IS NULL
if we would put JSON NULL into DB. So returning null
here is the better approach IMO.
// extracted from symfony's php 5.5 polyfill | ||
// @link https://github.com/symfony/polyfill-php55/blob/master/Php55.php | ||
// @link http://nl1.php.net/manual/en/function.json-last-error-msg.php | ||
private static function json_last_error_msg() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why static
? This method is not used in static context so we should make it non-static. Also please don't use underscore naming. I'd propose something like getLastErrorMessage()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed. I kind of copied the polyfill, and static seemed ok to be used here as it does not depend on any context. But I removed the static, as it doesn't really matter
@@ -99,4 +99,16 @@ static public function conversionFailedInvalidType($value, $toType, array $possi | |||
implode(', ', $possibleTypes) | |||
)); | |||
} | |||
|
|||
static public function conversionFailedSerialization($value, $format, $error, \Exception $previous = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we really need to introduce another method here. Maybe it would suffice the purpose by sticking to conversionFailedFormat()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @Ocramius @zeroedin-bill
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is also what I thought but as @zeroedin-bill suggested it, I went with it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Taluu yeah sorry then for the extra effort. Could you be so kind to just use conversionFailedFormat()
then? Also you might just put the error message into $expectedFormat
. Like:
<?php
// snippet
if (JSON_ERROR_NONE !== json_last_error()) {
throw ConversionException::conversionFailedFormat(
$value,
Type::JSON,
'json (' . $this->getLastErrorMessage() . ')'
);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Taluu ah no wait.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception message says Could not convert database value to Doctrine type
which would be wrong then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm leaving this as is then, unless something better can be found ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure the message format is good too though
Ping. Any news around here ? Thanks |
LGTM. /cc @zeroedin-bill @Ocramius merge? |
LGTM. @zeroedin-bill, can you check it and press the big green scary button? :-) |
Driving all day, sry
|
Assigned it to you - whenever you got time :-) Doesn't need merging back into 2.5, so it's as quick as the UI interaction, Marco Pivetta http://ocramius.github.com/ On 15 June 2016 at 17:21, Bill Schaller notifications@github.com wrote:
|
"Even though I walk through the valley of the shadow of death, I fear no evil" Merged... =) |
json
type that doesn't have the flaws of json_array
As discussed in #891, the
JsonArrayType
was deprecated in favor of a newJsonType
. :}