Skip to content
Snippets Groups Projects
Commit 19dff131 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

reorder code to ensure a correct order of callback execution, re #1990

Merge request studip/studip!1313
parent 1dfcb8b4
No related branches found
No related tags found
No related merge requests found
...@@ -1880,20 +1880,15 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -1880,20 +1880,15 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
} }
$ret = 0; $ret = 0;
$i18ncontent = [];
if (!$this->isDeleted() && ($this->isDirty() || $this->isNew())) { if (!$this->isDeleted() && ($this->isDirty() || $this->isNew())) {
if ($this->isNew()) { $callback = $this->isNew() ? 'before_create' : 'before_update';
if ($this->applyCallbacks('before_create') === false) { if ($this->applyCallbacks($callback) === false) {
return false; return false;
}
} else {
if ($this->applyCallbacks('before_update') === false) {
return false;
}
} }
// Collect i18n contents // Collect i18n contents
$i18ncontent = [];
foreach (array_keys($this->i18n_fields()) as $field) { foreach (array_keys($this->i18n_fields()) as $field) {
if ($this->content[$field] instanceof I18NString) { if ($this->content[$field] instanceof I18NString) {
$i18ncontent[$field] = $this->content[$field]; $i18ncontent[$field] = $this->content[$field];
...@@ -1902,6 +1897,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -1902,6 +1897,7 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
} }
} }
// Create sql data assignment chunks
foreach ($this->db_fields() as $field => $meta) { foreach ($this->db_fields() as $field => $meta) {
$value = $this->content[$field]; $value = $this->content[$field];
if ($field == 'chdate' && !$this->isFieldDirty($field) && $this->isDirty()) { if ($field == 'chdate' && !$this->isFieldDirty($field) && $this->isDirty()) {
...@@ -1925,6 +1921,8 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -1925,6 +1921,8 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
$this->content[$field] = $value; $this->content[$field] = $value;
$query_part[] = "`$field` = " . DBManager::get()->quote($value) . " "; $query_part[] = "`$field` = " . DBManager::get()->quote($value) . " ";
} }
// Create store query
if (!$this->isNew()) { if (!$this->isNew()) {
$where_query = $this->getWhereQuery(); $where_query = $this->getWhereQuery();
$query = "UPDATE `{$this->db_table()}` SET " $query = "UPDATE `{$this->db_table()}` SET "
...@@ -1936,20 +1934,15 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -1936,20 +1934,15 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
} }
$ret = DBManager::get()->exec($query); $ret = DBManager::get()->exec($query);
// Retrieve generated id from database if pk is an auto increment
// column
if ($this->isNew()) { if ($this->isNew()) {
if ($this->hasAutoIncrementColumn() && !$this->getId()) { if ($this->hasAutoIncrementColumn() && !$this->getId()) {
$this->setId(DBManager::get()->lastInsertId()); $this->setId(DBManager::get()->lastInsertId());
} }
$this->applyCallbacks('after_create');
} else {
$this->applyCallbacks('after_update');
} }
}
$rel_ret = $this->storeRelations();
// Store i18n contents // Store i18n contents
if (count($i18ncontent) > 0) {
foreach ($i18ncontent as $field => $one) { foreach ($i18ncontent as $field => $one) {
$meta = [ $meta = [
'object_id' => $this->getId(), 'object_id' => $this->getId(),
...@@ -1963,7 +1956,11 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate ...@@ -1963,7 +1956,11 @@ class SimpleORMap implements ArrayAccess, Countable, IteratorAggregate
$this->content_db[$field] = clone $one; $this->content_db[$field] = clone $one;
} }
} }
// Apply callbacks
$this->applyCallbacks($this->isNew() ? 'after_create' : 'after_update');
} }
$rel_ret = $this->storeRelations();
$this->applyCallbacks('after_store'); $this->applyCallbacks('after_store');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment