-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
[WIP] Nullable embedded objects. #1275
Changes from 3 commits
74d50f7
4ec25c8
93300bc
9cc7529
40dfa3f
6340eec
f48d05e
609d492
71d3580
6e57134
d27c5b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2564,6 +2564,8 @@ public function createEntity($className, array $data, &$hints = array()) | |
return $entity; | ||
} | ||
|
||
$this->removeNullableEmbeddedReferences($data, $class); | ||
|
||
foreach ($data as $field => $value) { | ||
if (isset($class->fieldMappings[$field])) { | ||
$class->reflFields[$field]->setValue($entity, $value); | ||
|
@@ -2767,6 +2769,34 @@ public function createEntity($className, array $data, &$hints = array()) | |
return $entity; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @param ClassMetadata $class | ||
*/ | ||
private function removeNullableEmbeddedReferences(array &$data, ClassMetadata $class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Ocramius I've tried to keep the things simple here, but if we have a huge amount of data or a large result set I think we might have a little delay to process. I just don't have any idea to improve this =/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about something like this: public function createEntity($className, array $data, &$hints = array())
{
// ...
//$this->removeNullableEmbeddedReferences($data, $class);
foreach ($data as $field => $value) {
if (null === $value && $this->isNullableEmbeddedField($class, $field)) {
continue;
}
if (isset($class->fieldMappings[$field])) {
$class->reflFields[$field]->setValue($entity, $value);
}
}
// ...
}
private function isNullableEmbeddedField(ClassMetadata $class, $field)
{
return isset(
$class->fieldMappings[$field]['declaredField'],
$class->embeddedClasses[$name = $class->fieldMappings[$field]['declaredField']]
) ? true === $class->embeddedClasses[$name]['nullable'] : false;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @1ed, its a good alternative. I'll play a lit bit now 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Ocramius, @1ed and @beberlei what do you think to extract that logic from I thought something like |
||
{ | ||
$nullables = array_keys( | ||
array_filter( | ||
$class->embeddedClasses, | ||
function (array $embedded) { | ||
return $embedded['nullable'] === true; | ||
} | ||
) | ||
); | ||
|
||
if (empty($nullables)) { | ||
return; | ||
} | ||
|
||
foreach ($data as $field => $value) { | ||
$property = substr($field, 0, strpos($field, '.')); | ||
|
||
if (in_array($property, $nullables) && $value === null) { | ||
unset($data[$field]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
|
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.
Is that extra parameters really required? Can't you use
$this->embeddedClasses[$property]['nullable']
in this method 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.
You're right, my mistake.