Commit ae166681 authored by Karamel's avatar Karamel
Browse files

Fix fromStruct association arrays, with [] or null to clear and not set to keep.

parent 4abff17e
......@@ -282,15 +282,19 @@ abstract class DoctrineModel
}
$fieldName = $field['name'];
// Check for required (not null)
if (empty($field['null']) && empty($struct[$fieldName])) {
if (empty($field['null'])
&& (!array_key_exists($fieldName, $struct) || $struct[$fieldName] === null)) {
if (empty($field['array'])) {
throw new \UnexpectedValueException(sprintf('%s is required', $fieldName));
} else {
// Allow ommiting empty arrays as 'keep'
// Allow omitting an array for keeping its value
// or setting it to null to clear it.
}
}
// Get value and assign it.
if (!empty($struct[$fieldName])) {
if (array_key_exists($fieldName, $struct) // is set
&& $struct[$fieldName] !== null // and not null
&& (!is_array($struct[$fieldName]) || count($struct[$fieldName] == 0))) { // and not an empty array
$value = $model->readAssociationValue($struct, $dao, $field);
if (!empty($field['array'])) {
/* At that point the model is filled from database by Doctrine.
......@@ -349,7 +353,8 @@ abstract class DoctrineModel
$setter = $model->findMethodNameOrThrow('set', $fieldName);
call_user_func([$model, $setter], $value);
}
} else if (isset($struct[$fieldName]) && $struct[$fieldName] === null) {
} else if (array_key_exists($fieldName, $struct)) {
// either null or an empty array
$setter = $model->findMethodNameOrThrow('set', $fieldName);
// Delete association on explicit null value
if (empty($field['array'])) {
......@@ -359,7 +364,7 @@ abstract class DoctrineModel
// Clear array for arrays
call_user_func([$model, $setter], []);
}
} // Empty an not null: keep the current association value
} // Not set: keep the current association value which was set by Doctrine
}
return $model;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment