Form Controller now pushes values to relations

This commit is contained in:
Sam Georges 2014-05-18 15:39:12 +10:00
parent 5484f67c5a
commit 786a833237

View File

@ -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;
}
}
}