mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Add support for deferred binding for belongsTo relations - Refs #1329
This commit is contained in:
parent
aaebea4716
commit
ce4636d59a
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user