Commit 1d867a92 authored by Tomas Lang's avatar Tomas Lang

Entity: FindEntity trait

parent 839042b0
......@@ -14,12 +14,13 @@ class Repository extends AbstractRepository
/**
* @param int|Restriction $id
* @param callback|null $notFoundCallaback
*
* @return Entity
*
* @throws InvalidArgumentException
*/
public function find($id)
public function find($id, $notFoundCallback = NULL)
{
if (is_numeric($id)) {
$restriction = $this->createRestriction();
......@@ -35,7 +36,11 @@ class Repository extends AbstractRepository
$row = $statement->fetch();
if ($row === FALSE) {
throw new \InvalidArgumentException('Not found: ' . var_export($id, TRUE));
if (is_callable($notFoundCallback)) {
\Nette\Utils\Callback::invokeArgs($notFoundCallback, [$id]);
} else {
throw new InvalidArgumentException('Not found: ' . var_export($id, TRUE));
}
}
return $this->createEntity($row);
......
<?php
namespace LeanMapperWorkflow\Traits;
use LeanMapperWorkflow\Repository;
trait FindEntity
{
/**
* @param Repository $repository
* @param mixed $id
* @param bool $showMessage
* @param string $redirectLink
*
* @throws \InvalidArgumentException
*/
protected final function findEntity(Repository $repository, $id, $showMessage = FALSE, $redirectLink = NULL)
{
if ($this instanceof \Nette\Application\IPresenter && ($showMessage !== FALSE || $redirectLink !== NULL)) {
$callback = function() use ($showMessage, $redirectLink) {
if ($showMessage) {
$this->flashMessage('@todo', 'error');
}
if ($link !== NULL) {
$this->redirect($link);
}
};
} else {
$callback = NULL;
}
return $repository->find($id, $callback);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment