Skip to content

Commit

Permalink
Update Document repository
Browse files Browse the repository at this point in the history
  • Loading branch information
webeweb committed May 28, 2024
1 parent ce97155 commit ff6013d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function dispatchDocumentEvent(string $eventName, DocumentInterface $d
*/
protected function findDocument(?int $id, bool $ex): ?DocumentInterface {

$document = $this->getEntityManager()->getRepository(Document::class)->find($id);
$document = $this->getEntityManager()->getRepository(Document::class)->findOneById($id);
if (null === $document && true === $ex) {
throw new NotFoundHttpException();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/DropzoneController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function indexAction(int $id = null): Response {
/** @var DocumentRepository $repository */
$repository = $this->getEntityManager()->getRepository(Document::class);

$directory = $repository->find($id);
$directory = $repository->findOneById($id);
$entities = $repository->findAllDocumentsByParent($directory);

return new JsonResponse($entities);
Expand Down Expand Up @@ -83,7 +83,7 @@ public function serializeAction(int $id): Response {
*/
public function uploadAction(Request $request, int $id = null): Response {

$parent = $this->getEntityManager()->getRepository(Document::class)->find($id);
$parent = $this->getEntityManager()->getRepository(Document::class)->findOneById($id);

$document = new Document();
$document->setCreatedAt(new DateTime());
Expand Down
44 changes: 27 additions & 17 deletions src/Repository/DocumentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
namespace WBW\Bundle\EDMBundle\Repository;

use Doctrine\ORM\QueryBuilder;
use Throwable;
use WBW\Bundle\DataTablesBundle\Model\DataTablesWrapperInterface;
use WBW\Bundle\DataTablesBundle\Repository\DefaultDataTablesRepository;
use WBW\Bundle\EDMBundle\Entity\Document;
use WBW\Bundle\EDMBundle\Model\DocumentInterface;

/**
Expand Down Expand Up @@ -74,23 +76,6 @@ protected function dataTablesFindAllQueryBuilder(DataTablesWrapperInterface $dtW
return $this->appendWhereParent($dtWrapper, $qb);
}

/**
* {@inheritDoc}
*/
public function find($id, $lockMode = null, $lockVersion = null) {

$qb = $this->createQueryBuilder("d");
$qb->leftJoin("d.parent", "p")
->addSelect("p")
->leftJoin("d.children", "c")
->addSelect("c")
->andWhere("d.id = :id")
->setParameter(":id", $id)
->orderBy("d.name", "ASC");

return $qb->getQuery()->getOneOrNullResult();
}

/**
* Find all by parent.
*
Expand Down Expand Up @@ -168,6 +153,31 @@ public function findAllDocumentsByParent(?DocumentInterface $parent): array {
return $qb->getQuery()->getResult();
}

/**
* Find one by id.
*
* @param int|null $id The id.
* @return Document|null Returns the document.
* @throws Throwable Throws an exception if an error occurs.
*/
public function findOneById(?int $id): ?Document {

if (null === $id) {
return null;
}

$qb = $this->createQueryBuilder("d");
$qb->leftJoin("d.parent", "p")
->addSelect("p")
->leftJoin("d.children", "c")
->addSelect("c")
->andWhere("d.id = :id")
->setParameter(":id", $id)
->orderBy("d.name", "ASC");

return $qb->getQuery()->getOneOrNullResult();
}

/**
* Remove orphans.
*
Expand Down

0 comments on commit ff6013d

Please sign in to comment.