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());