diff options
| author | Moritz Strohm <strohm@data-quest.de> | 2022-05-04 14:18:41 +0000 |
|---|---|---|
| committer | Moritz Strohm <strohm@data-quest.de> | 2022-05-04 14:18:41 +0000 |
| commit | 0d95625c2ab0eabe98e724ab79be0087093cc35b (patch) | |
| tree | f911e16bfb52f4aceacf9e9ad4c8d31923e26a6e /lib/models/SimpleORMap.class.php | |
| parent | e1f835675363c17fbf5d269f6d1d1f84b6727886 (diff) | |
fix for BIESt #957
Merge request studip/studip!554
Diffstat (limited to 'lib/models/SimpleORMap.class.php')
| -rw-r--r-- | lib/models/SimpleORMap.class.php | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/lib/models/SimpleORMap.class.php b/lib/models/SimpleORMap.class.php index cc51138..16eeadf 100644 --- a/lib/models/SimpleORMap.class.php +++ b/lib/models/SimpleORMap.class.php @@ -241,7 +241,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate } foreach (['has_many', 'belongs_to', 'has_one', 'has_and_belongs_to_many'] as $type) { - if (is_array($config[$type])) { + if (isset($config[$type]) && is_array($config[$type])) { foreach (array_keys($config[$type]) as $one) { $config['relations'][$one] = null; } @@ -310,9 +310,9 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate $config['known_slots'] = array_merge( array_keys($config['db_fields']), - array_keys($config['alias_fields'] ?: []), - array_keys($config['additional_fields'] ?: []), - array_keys($config['relations'] ?: []) + array_keys($config['alias_fields'] ?? []), + array_keys($config['additional_fields'] ?? []), + array_keys($config['relations'] ?? []) ); foreach (array_map('strtolower', get_class_methods($class)) as $method) { @@ -339,7 +339,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate static::configure(); } - return self::$config[static::class][$key]; + return self::$config[static::class][$key] ?? null; } /** @@ -827,6 +827,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate $db_fields = static::config('db_fields'); $name = strtolower($name); $class = get_called_class(); + $order = ''; $param_arr = []; $where = ''; $where_param = is_array($arguments[0]) ? $arguments[0] : [$arguments[0]]; @@ -834,21 +835,21 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate $field = substr($name, strlen($prefix) + 2); switch ($prefix) { case 'findone': - $order = $arguments[1]; + $order = $arguments[1] ?? ''; $param_arr[0] =& $where; $param_arr[1] = [$where_param]; $method = 'findonebysql'; break; case 'find': case 'findmany': - $order = $arguments[1]; + $order = $arguments[1] ?? ''; $param_arr[0] =& $where; $param_arr[1] = [$where_param]; $method = 'findbysql'; break; case 'findeach': case 'findeachmany': - $order = $arguments[2]; + $order = $arguments[2] ?? ''; $param_arr[0] = $arguments[0]; $param_arr[1] =& $where; $param_arr[2] = [$arguments[1]]; @@ -1024,10 +1025,10 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate * @return array */ protected function parseRelationOptions($type, $name, $options) { - if (!$options['class_name']) { + if (empty($options['class_name'])) { throw new Exception('Option class_name not set for relation ' . $name); } - if (!$options['assoc_foreign_key']) { + if (empty($options['assoc_foreign_key'])) { if ($type === 'has_many' || $type === 'has_one') { $options['assoc_foreign_key'] = $this->pk[0]; } else if ($type === 'belongs_to') { @@ -1062,28 +1063,28 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate throw new Exception("For relation " . $name . " assoc_foreign_key must be a name of a column"); } } - if (!$options['assoc_func']) { + if (empty($options['assoc_func'])) { if ($type !== 'has_and_belongs_to_many') { $options['assoc_func'] = $options['assoc_foreign_key'] === 'id' ? 'find' : 'findBy' . $options['assoc_foreign_key']; } else { $options['assoc_func'] = 'findThru'; } } - if (!$options['foreign_key']) { + if (empty($options['foreign_key'])) { $options['foreign_key'] = 'id'; } - if ($options['foreign_key'] instanceof Closure) { + if (isset($options['foreign_key']) && $options['foreign_key'] instanceof Closure) { $options['assoc_func_params_func'] = function($record) use ($name, $options) { return call_user_func($options['foreign_key'], $record, $name, $options);}; } else { $options['assoc_func_params_func'] = function($record) use ($name, $options) { return $options['foreign_key'] === 'id' ? $record->getId() : $record->getValue($options['foreign_key']);}; } - if ($options['assoc_foreign_key'] instanceof Closure) { + if (isset($options['assoc_foreign_key']) && $options['assoc_foreign_key'] instanceof Closure) { if ($type === 'belongs_to') { $options['assoc_foreign_key_getter'] = function($record, $that) use ($name, $options) { return call_user_func($options['assoc_foreign_key'], $record, $name, $options, $that);}; } else { $options['assoc_foreign_key_setter'] = function($record, $params) use ($name, $options) { return call_user_func($options['assoc_foreign_key'], $record, $params, $name, $options);}; } - } elseif ($options['assoc_foreign_key']) { + } elseif (!empty($options['assoc_foreign_key'])) { if ($type === 'belongs_to') { $options['assoc_foreign_key_getter'] = function($record, $that) use ($name, $options) { return $record->getValue($options['assoc_foreign_key']);}; } else { @@ -1188,7 +1189,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate function getId() { if (count($this->pk) == 1) { - return $this->content[$this->pk[0]]; + return $this->content[$this->pk[0]] ?? null; } else { $id = []; foreach ($this->pk as $key) { @@ -1758,7 +1759,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate $where_query = null; $pk_not_set = []; foreach ($this->pk as $key) { - $pk = $this->content_db[$key] ?: $this->content[$key]; + $pk = $this->content_db[$key] ?? $this->content[$key] ?? null; if (isset($pk)) { $where_query[] = "`{$this->db_table}`.`{$key}` = " . DBManager::get()->quote($pk); } else { @@ -2134,7 +2135,10 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate } $params = $options['assoc_func_params_func']; if ($options['type'] === 'has_many') { - $records = function($record) use ($to_call, $params, $options) {$p = (array)$params($record); return call_user_func_array($to_call, array_merge(count($p) ? $p : [null], [$options['order_by']]));}; + $records = function($record) use ($to_call, $params, $options) { + $p = (array)$params($record); + return call_user_func_array($to_call, array_merge(count($p) ? $p : [null], [$options['order_by'] ?? ''])); + }; $this->relations[$relation] = new SimpleORMapCollection($records, $options, $this); } elseif ($options['type'] === 'has_and_belongs_to_many') { $records = function($record) use ($to_call, $params, $options) {$p = (array)$params($record); return call_user_func_array($to_call, array_merge(count($p) ? $p : [null], [$options]));}; |
