diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2024-05-06 07:37:00 +0000 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2024-05-06 07:37:00 +0000 |
| commit | b9ac604f3b4f03acc8ce4a88c78fbb399be3a373 (patch) | |
| tree | e69692e4562b5bf88a0e5bac5a6676e10878e0c1 /lib | |
| parent | 329ab7b0e2fb946bb4773612b42ec47370cc5d43 (diff) | |
fixes #4100
Closes #4100
Merge request studip/studip!2950
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/models/SimpleORMap.class.php | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/lib/models/SimpleORMap.class.php b/lib/models/SimpleORMap.class.php index 89cdd09..2013db4 100644 --- a/lib/models/SimpleORMap.class.php +++ b/lib/models/SimpleORMap.class.php @@ -568,10 +568,11 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate $record_data = []; $relation_data = []; foreach ($data as $key => $value) { - if (is_array($value)) { - $relation_data[$key] = $value; - } else { + $temp = static::alias_fields()[$key] ?? $key; + if (isset(static::db_fields()[$temp])) { $record_data[$key] = $value; + } else { + $relation_data[$key] = $value; } } $record = static::toObject($record_data); @@ -581,29 +582,31 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate } else { $record->setData($record_data); } - if (is_array($relation_data)) { - foreach ($relation_data as $relation => $data) { - $options = $record->getRelationOptions($relation); - if ($options['type'] == 'has_one') { - $record->{$relation} = call_user_func([$options['class_name'], 'import'], $data); - } - if ($options['type'] == 'has_many' || $options['type'] == 'has_and_belongs_to_many') { - foreach ($data as $one) { - $current = call_user_func([$options['class_name'], 'import'], $one); - if ($options['type'] == 'has_many') { - $foreign_key_value = call_user_func($options['assoc_func_params_func'], $record); - call_user_func($options['assoc_foreign_key_setter'], $current, $foreign_key_value); - } - if ($current->id !== null) { - $existing = $record->{$relation}->find($current->id); - if ($existing) { - $existing->setData($current); - } else { - $record->{$relation}->append($current); - } + foreach ($relation_data as $relation => $data) { + if (!$record->isRelation($relation)) { + continue; + } + + $options = $record->getRelationOptions($relation); + if ($options['type'] == 'has_one') { + $record->{$relation} = call_user_func([$options['class_name'], 'import'], $data); + } + if ($options['type'] == 'has_many' || $options['type'] == 'has_and_belongs_to_many') { + foreach ($data as $one) { + $current = call_user_func([$options['class_name'], 'import'], $one); + if ($options['type'] == 'has_many') { + $foreign_key_value = call_user_func($options['assoc_func_params_func'], $record); + call_user_func($options['assoc_foreign_key_setter'], $current, $foreign_key_value); + } + if ($current->id !== null) { + $existing = $record->{$relation}->find($current->id); + if ($existing) { + $existing->setData($current); } else { $record->{$relation}->append($current); } + } else { + $record->{$relation}->append($current); } } } |
