Skip to content
Snippets Groups Projects
Commit 8b9193f3 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms Committed by Jan-Hendrik Willms
Browse files

fixes #4100

Closes #4100

Merge request studip/studip!2950
parent 77a43b34
No related branches found
No related tags found
No related merge requests found
...@@ -568,10 +568,11 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -568,10 +568,11 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
$record_data = []; $record_data = [];
$relation_data = []; $relation_data = [];
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
if (is_array($value)) { $temp = static::alias_fields()[$key] ?? $key;
$relation_data[$key] = $value; if (isset(static::db_fields()[$temp])) {
} else {
$record_data[$key] = $value; $record_data[$key] = $value;
} else {
$relation_data[$key] = $value;
} }
} }
$record = static::toObject($record_data); $record = static::toObject($record_data);
...@@ -581,29 +582,31 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -581,29 +582,31 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
} else { } else {
$record->setData($record_data); $record->setData($record_data);
} }
if (is_array($relation_data)) { foreach ($relation_data as $relation => $data) {
foreach ($relation_data as $relation => $data) { if (!$record->isRelation($relation)) {
$options = $record->getRelationOptions($relation); continue;
if ($options['type'] == 'has_one') { }
$record->{$relation} = call_user_func([$options['class_name'], 'import'], $data);
} $options = $record->getRelationOptions($relation);
if ($options['type'] == 'has_many' || $options['type'] == 'has_and_belongs_to_many') { if ($options['type'] == 'has_one') {
foreach ($data as $one) { $record->{$relation} = call_user_func([$options['class_name'], 'import'], $data);
$current = call_user_func([$options['class_name'], 'import'], $one); }
if ($options['type'] == 'has_many') { if ($options['type'] == 'has_many' || $options['type'] == 'has_and_belongs_to_many') {
$foreign_key_value = call_user_func($options['assoc_func_params_func'], $record); foreach ($data as $one) {
call_user_func($options['assoc_foreign_key_setter'], $current, $foreign_key_value); $current = call_user_func([$options['class_name'], 'import'], $one);
} if ($options['type'] == 'has_many') {
if ($current->id !== null) { $foreign_key_value = call_user_func($options['assoc_func_params_func'], $record);
$existing = $record->{$relation}->find($current->id); call_user_func($options['assoc_foreign_key_setter'], $current, $foreign_key_value);
if ($existing) { }
$existing->setData($current); if ($current->id !== null) {
} else { $existing = $record->{$relation}->find($current->id);
$record->{$relation}->append($current); if ($existing) {
} $existing->setData($current);
} else { } else {
$record->{$relation}->append($current); $record->{$relation}->append($current);
} }
} else {
$record->{$relation}->append($current);
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment