From b9581332aa7b5b51474077342418f73045e46cb3 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 6 Dec 2017 21:21:19 +1100 Subject: [PATCH] Save relations first in model saver This aligns better with the relation principal "parent saves child" / "child cannot save parent" and is more conducive to the natural workflow of a coder, ie // Relation first $gallery = new Gallery; $gallery->save(); // Primary model last $post = new Post; $post->gallery = $gallery; $post->save(); Refs https://github.com/octobercms/library/pull/277 --- modules/backend/traits/FormModelSaver.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/modules/backend/traits/FormModelSaver.php b/modules/backend/traits/FormModelSaver.php index 2df87ab7c..17a422e22 100644 --- a/modules/backend/traits/FormModelSaver.php +++ b/modules/backend/traits/FormModelSaver.php @@ -13,7 +13,6 @@ use October\Rain\Database\Model as DatabaseModel; * @package october\backend * @author Alexey Bobkov, Samuel Georges */ - trait FormModelSaver { /** @@ -40,11 +39,15 @@ trait FormModelSaver { $this->modelsToSave = []; $this->setModelAttributes($model, $saveData); + $this->modelsToSave = array_reverse($this->modelsToSave); return $this->modelsToSave; } /** - * Sets a data collection to a model attributes, relations will also be set. + * Sets a data collection to a model attributes, relations are also set. + * + * @param \October\Rain\Database\Model $model Model to fill. + * @param array $saveData Attribute values to fill model. * @return void */ protected function setModelAttributes($model, $saveData) @@ -85,6 +88,14 @@ trait FormModelSaver } } + /** + * Removes an array of attributes from the model. If the model implements + * the Purgeable trait, this is preferred over the internal logic. + * + * @param \October\Rain\Database\Model $model Model to adjust. + * @param array $attributesToPurge Attribute values to remove from the model. + * @return void + */ protected function deferPurgedSaveAttributes($model, $attributesToPurge) { if (!is_array($attributesToPurge)) {