Skip to content

Commit

Permalink
RepositoryForm: Introduce method getBaseTable()
Browse files Browse the repository at this point in the history
Concrete form classes may want to utilize another base table
than the one of the repository.
  • Loading branch information
Johannes Meyer committed Apr 29, 2016
1 parent 21e4ba5 commit 9c097c3
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions application/forms/RepositoryForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;

Expand All @@ -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
*
Expand Down Expand Up @@ -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());
}
Expand All @@ -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());
}
}
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit 9c097c3

Please sign in to comment.