If the pivot model fails for some reason, abort the sync

Refs #1408
This commit is contained in:
Samuel Georges 2015-09-12 07:53:33 +10:00
parent 1d5caee2d9
commit e8b4bf9d72

View File

@ -1112,24 +1112,29 @@ class RelationController extends ControllerBehavior
$this->beforeAjax();
/*
* Add the checked IDs to the pivot table
* If the pivot model fails for some reason, abort the sync
*/
$foreignIds = (array) $this->foreignId;
$this->relationObject->sync($foreignIds, false);
Db::transaction(function () {
/*
* Add the checked IDs to the pivot table
*/
$foreignIds = (array) $this->foreignId;
$this->relationObject->sync($foreignIds, false);
/*
* Save data to models
*/
$foreignKeyName = $this->relationModel->getQualifiedKeyName();
$hyrdatedModels = $this->relationObject->whereIn($foreignKeyName, $foreignIds)->get();
$saveData = $this->pivotWidget->getSaveData();
/*
* Save data to models
*/
$foreignKeyName = $this->relationModel->getQualifiedKeyName();
$hyrdatedModels = $this->relationObject->whereIn($foreignKeyName, $foreignIds)->get();
$saveData = $this->pivotWidget->getSaveData();
foreach ($hyrdatedModels as $hydratedModel) {
$modelsToSave = $this->prepareModelsToSave($hydratedModel, $saveData);
foreach ($modelsToSave as $modelToSave) {
$modelToSave->save();
foreach ($hyrdatedModels as $hydratedModel) {
$modelsToSave = $this->prepareModelsToSave($hydratedModel, $saveData);
foreach ($modelsToSave as $modelToSave) {
$modelToSave->save();
}
}
}
});
return ['#'.$this->relationGetId('view') => $this->relationRenderView()];
}