From 9c097c3957846d0a1a8b4f43ca89dc0e2c406f5c Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 29 Apr 2016 09:31:26 +0200 Subject: [PATCH] RepositoryForm: Introduce method getBaseTable() Concrete form classes may want to utilize another base table than the one of the repository. --- application/forms/RepositoryForm.php | 54 +++++++++++++++++++--------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/application/forms/RepositoryForm.php b/application/forms/RepositoryForm.php index 05cf0ac195..8626560f6d 100644 --- a/application/forms/RepositoryForm.php +++ b/application/forms/RepositoryForm.php @@ -33,10 +33,17 @@ abstract class RepositoryForm extends Form /** * The repository being worked with * - * @var Repository + * @var Repository */ protected $repository; + /** + * The target being worked with + * + * @var mixed + */ + protected $baseTable; + /** * How to interact with the repository * @@ -54,7 +61,7 @@ abstract class RepositoryForm extends Form /** * The data of the entry to pre-populate the form with when in mode insert or update * - * @var type + * @var type */ protected $data; @@ -71,6 +78,20 @@ public function setRepository(Repository $repository) return $this; } + /** + * Return the target being worked with + * + * @return mixed + */ + protected function getBaseTable() + { + if ($this->baseTable === null) { + return $this->repository->getBaseTable(); + } + + return $this->baseTable; + } + /** * Return the name of the entry to handle * @@ -219,7 +240,11 @@ protected function onUpdateRequest() { $data = $this->getData(); if (empty($data)) { - $row = $this->repository->select()->applyFilter($this->createFilter())->fetchRow(); + $row = $this->repository + ->select() + ->from($this->getBaseTable()) + ->applyFilter($this->createFilter()) + ->fetchRow(); if ($row === false) { throw new NotFoundError('Entry "%s" not found', $this->getIdentifier()); } @@ -239,7 +264,12 @@ protected function onUpdateRequest() */ protected function onDeleteRequest() { - if ($this->repository->select()->addFilter($this->createFilter())->count() === 0) { + $count = $this->repository + ->select() + ->from($this->getBaseTable()) + ->addFilter($this->createFilter()) + ->count(); + if ($count === 0) { throw new NotFoundError('Entry "%s" not found', $this->getIdentifier()); } } @@ -268,10 +298,7 @@ public function onSuccess() protected function onInsertSuccess() { try { - $this->repository->insert( - $this->repository->getBaseTable(), - $this->getValues() - ); + $this->repository->insert($this->getBaseTable(), $this->getValues()); } catch (Exception $e) { Notification::error($this->getInsertMessage(false)); $this->error($e->getMessage()); @@ -290,11 +317,7 @@ protected function onInsertSuccess() protected function onUpdateSuccess() { try { - $this->repository->update( - $this->repository->getBaseTable(), - $this->getValues(), - $this->createFilter() - ); + $this->repository->update($this->getBaseTable(), $this->getValues(), $this->createFilter()); } catch (Exception $e) { Notification::error($this->getUpdateMessage(false)); $this->error($e->getMessage()); @@ -313,10 +336,7 @@ protected function onUpdateSuccess() protected function onDeleteSuccess() { try { - $this->repository->delete( - $this->repository->getBaseTable(), - $this->createFilter() - ); + $this->repository->delete($this->getBaseTable(), $this->createFilter()); } catch (Exception $e) { Notification::error($this->getDeleteMessage(false)); $this->error($e->getMessage());