aboutsummaryrefslogtreecommitdiff
path: root/lib/models/SimpleORMap.class.php
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2023-01-13 15:06:20 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2023-01-13 15:06:20 +0000
commit19dff131eef31e8718adf28553b6623a36532d78 (patch)
tree102c2963c34828ce66ba73937e3bdd8ff60faf53 /lib/models/SimpleORMap.class.php
parent1dfcb8b4b2637c0e6a3ecd678a5dbf37c5d137bf (diff)
reorder code to ensure a correct order of callback execution, re #1990
Merge request studip/studip!1313
Diffstat (limited to 'lib/models/SimpleORMap.class.php')
-rw-r--r--lib/models/SimpleORMap.class.php31
1 files changed, 14 insertions, 17 deletions
diff --git a/lib/models/SimpleORMap.class.php b/lib/models/SimpleORMap.class.php
index fe77d45..7af9dec 100644
--- a/lib/models/SimpleORMap.class.php
+++ b/lib/models/SimpleORMap.class.php
@@ -1880,20 +1880,15 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
}
$ret = 0;
- $i18ncontent = [];
if (!$this->isDeleted() && ($this->isDirty() || $this->isNew())) {
- if ($this->isNew()) {
- if ($this->applyCallbacks('before_create') === false) {
- return false;
- }
- } else {
- if ($this->applyCallbacks('before_update') === false) {
- return false;
- }
+ $callback = $this->isNew() ? 'before_create' : 'before_update';
+ if ($this->applyCallbacks($callback) === false) {
+ return false;
}
// Collect i18n contents
+ $i18ncontent = [];
foreach (array_keys($this->i18n_fields()) as $field) {
if ($this->content[$field] instanceof I18NString) {
$i18ncontent[$field] = $this->content[$field];
@@ -1902,6 +1897,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
}
}
+ // Create sql data assignment chunks
foreach ($this->db_fields() as $field => $meta) {
$value = $this->content[$field];
if ($field == 'chdate' && !$this->isFieldDirty($field) && $this->isDirty()) {
@@ -1925,6 +1921,8 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
$this->content[$field] = $value;
$query_part[] = "`$field` = " . DBManager::get()->quote($value) . " ";
}
+
+ // Create store query
if (!$this->isNew()) {
$where_query = $this->getWhereQuery();
$query = "UPDATE `{$this->db_table()}` SET "
@@ -1936,20 +1934,15 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
}
$ret = DBManager::get()->exec($query);
+ // Retrieve generated id from database if pk is an auto increment
+ // column
if ($this->isNew()) {
if ($this->hasAutoIncrementColumn() && !$this->getId()) {
$this->setId(DBManager::get()->lastInsertId());
}
-
- $this->applyCallbacks('after_create');
- } else {
- $this->applyCallbacks('after_update');
}
- }
- $rel_ret = $this->storeRelations();
- // Store i18n contents
- if (count($i18ncontent) > 0) {
+ // Store i18n contents
foreach ($i18ncontent as $field => $one) {
$meta = [
'object_id' => $this->getId(),
@@ -1963,7 +1956,11 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
$this->content_db[$field] = clone $one;
}
}
+
+ // Apply callbacks
+ $this->applyCallbacks($this->isNew() ? 'after_create' : 'after_update');
}
+ $rel_ret = $this->storeRelations();
$this->applyCallbacks('after_store');