mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Form Controller now pushes values to relations
This commit is contained in:
parent
5484f67c5a
commit
786a833237
@ -130,11 +130,8 @@ class FormController extends ControllerBehavior
|
||||
$this->controller->formBeforeSave($model);
|
||||
$this->controller->formBeforeCreateSave($model);
|
||||
|
||||
$saveData = $this->formWidget->getSaveData();
|
||||
foreach ($saveData as $attribute => $value) {
|
||||
$model->{$attribute} = $value;
|
||||
}
|
||||
$model->save(null, $this->formWidget->getSessionKey());
|
||||
$this->setModelAttributes($model, $this->formWidget->getSaveData());
|
||||
$model->push($this->formWidget->getSessionKey());
|
||||
|
||||
$this->controller->formAfterSave($model);
|
||||
|
||||
@ -182,11 +179,8 @@ class FormController extends ControllerBehavior
|
||||
$this->controller->formBeforeSave($model);
|
||||
$this->controller->formBeforeEditSave($model);
|
||||
|
||||
$saveData = $this->formWidget->getSaveData();
|
||||
foreach ($saveData as $attribute => $value) {
|
||||
$model->{$attribute} = $value;
|
||||
}
|
||||
$model->save(null, $this->formWidget->getSessionKey());
|
||||
$this->setModelAttributes($model, $this->formWidget->getSaveData());
|
||||
$model->push($this->formWidget->getSessionKey());
|
||||
|
||||
$this->controller->formAfterSave($model);
|
||||
|
||||
@ -519,4 +513,25 @@ class FormController extends ControllerBehavior
|
||||
*/
|
||||
public function formExtendQuery($query) {}
|
||||
|
||||
//
|
||||
// Internals
|
||||
//
|
||||
|
||||
/**
|
||||
* Sets a data collection to a model attributes, relations will also be set.
|
||||
* @param array $saveData Data to save.
|
||||
* @param Model $model Model to save to
|
||||
* @return void
|
||||
*/
|
||||
private function setModelAttributes($model, $saveData)
|
||||
{
|
||||
$singularTypes = ['belongsTo', 'hasOne', 'morphOne'];
|
||||
foreach ($saveData as $attribute => $value) {
|
||||
if ($model->hasRelation($attribute) && in_array($model->getRelationType($attribute), $singularTypes))
|
||||
$this->setModelAttributes($model->{$attribute}, $value);
|
||||
else
|
||||
$model->{$attribute} = $value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user