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
This commit is contained in:
parent
2036823eee
commit
b9581332aa
|
|
@ -13,7 +13,6 @@ use October\Rain\Database\Model as DatabaseModel;
|
||||||
* @package october\backend
|
* @package october\backend
|
||||||
* @author Alexey Bobkov, Samuel Georges
|
* @author Alexey Bobkov, Samuel Georges
|
||||||
*/
|
*/
|
||||||
|
|
||||||
trait FormModelSaver
|
trait FormModelSaver
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
|
@ -40,11 +39,15 @@ trait FormModelSaver
|
||||||
{
|
{
|
||||||
$this->modelsToSave = [];
|
$this->modelsToSave = [];
|
||||||
$this->setModelAttributes($model, $saveData);
|
$this->setModelAttributes($model, $saveData);
|
||||||
|
$this->modelsToSave = array_reverse($this->modelsToSave);
|
||||||
return $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
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function setModelAttributes($model, $saveData)
|
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)
|
protected function deferPurgedSaveAttributes($model, $attributesToPurge)
|
||||||
{
|
{
|
||||||
if (!is_array($attributesToPurge)) {
|
if (!is_array($attributesToPurge)) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue