From a1998e4348aad945666326c07998814ebaa67716 Mon Sep 17 00:00:00 2001 From: Sam Georges Date: Tue, 5 Aug 2014 18:20:25 +1000 Subject: [PATCH] Fixes #500 - Handle custom related key names, don't use session keys for existing parent models --- .../backend/behaviors/RelationController.php | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index 0f479d814..8e240f4e2 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -408,14 +408,16 @@ class RelationController extends ControllerBehavior */ protected function findExistingRelationIds($checkIds = null) { + $foreignKeyName = $this->relationModel->getKey(); + $results = $this->relationObject ->getBaseQuery() - ->select('id'); + ->select($foreignKeyName); if ($checkIds !== null && is_array($checkIds) && count($checkIds)) - $results = $results->whereIn('id', $checkIds); + $results = $results->whereIn($foreignKeyName, $checkIds); - return $results->lists('id'); + return $results->lists($foreignKeyName); } // @@ -494,10 +496,15 @@ class RelationController extends ControllerBehavior */ $existingIds = $this->findExistingRelationIds($checkedIds); $checkedIds = array_diff($checkedIds, $existingIds); + $foreignKeyName = $this->relationModel->getKey(); - $models = $this->relationModel->whereIn('id', $checkedIds)->get(); + $models = $this->relationModel->whereIn($foreignKeyName, $checkedIds)->get(); foreach ($models as $model) { - $this->relationObject->add($model, $this->relationGetSessionKey()); + + if ($this->model->exists) + $this->relationObject->add($model); + else + $this->relationObject->add($model, $this->relationGetSessionKey()); } } @@ -540,7 +547,8 @@ class RelationController extends ControllerBehavior /* * Check for existing relation */ - $existing = $this->relationObject->where('id', $foreignId)->count(); + $foreignKeyName = $this->relationModel->getKey(); + $existing = $this->relationObject->where($foreignKeyName, $foreignId)->count(); if (!$existing) $this->relationObject->add($foreignModel, null, $saveData);