Add support for deferred binding for belongsTo relations - Refs #1329

This commit is contained in:
Samuel Georges 2015-08-01 11:34:22 +10:00
parent aaebea4716
commit ce4636d59a

View File

@ -975,6 +975,7 @@ class RelationController extends ControllerBehavior
$this->beforeAjax();
$recordId = post('record_id');
$sessionKey = $this->deferredBinding ? $this->relationGetSessionKey() : null;
/*
* Add
@ -993,11 +994,6 @@ class RelationController extends ControllerBehavior
$models = $this->relationModel->whereIn($foreignKeyName, $checkedIds)->get();
foreach ($models as $model) {
$sessionKey = $this->deferredBinding
? $this->relationGetSessionKey()
: null;
$this->relationObject->add($model, $sessionKey);
}
}
@ -1009,15 +1005,20 @@ class RelationController extends ControllerBehavior
elseif ($this->viewMode == 'single') {
if ($recordId && ($model = $this->relationModel->find($recordId))) {
if ($this->relationType == 'belongsTo') {
$this->relationObject->associate($model);
$this->relationObject->getParent()->save();
}
elseif ($this->relationType == 'hasOne') {
$this->relationObject->add($model);
}
$this->relationObject->add($model, $sessionKey);
$this->viewWidget->setFormValues($model->attributes);
/*
* Belongs to relations won't save when using add() so
* it should occur if the conditions are right.
*/
if ($this->relationType == 'belongsTo') {
$parentModel = $this->relationObject->getParent();
if ($parentModel->exists && !$this->deferredBinding) {
$parentModel->save();
}
}
}
}