CategoryDao.php 965 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace internship\model;
  3. use n2n\context\attribute\RequestScoped;
  4. use n2n\persistence\orm\EntityManager;
  5. use internship\bo\Category;
  6. use n2n\context\attribute\Inject;
  7. #[RequestScoped]
  8. class CategoryDao {
  9. #[Inject]
  10. private EntityManager $em;
  11. /**
  12. * Gebe alle {@see Category}-Objekte, nach id absteigend sortiert, zurück.
  13. *
  14. * @return Category[]
  15. */
  16. function getCategories(): array {
  17. $criteria = $this->em->createSimpleCriteria(Category::getClass(), array(), array('id' => 'DESC'));
  18. return $criteria->toQuery()->fetchArray();
  19. }
  20. /**
  21. * Gebe den {@see Category} mit der enstprechenden ID zurück.
  22. */
  23. function getCategoryById(int $id): ?Category {
  24. return $this->em->find(Category::getClass(), $id);
  25. }
  26. function saveCategory(Category $newCategory): void {
  27. $this->em->persist($newCategory);
  28. }
  29. function removeCategory(int $articleId): void {
  30. $category = $this->getCategoryById($articleId);
  31. $this->em->remove($category);
  32. }
  33. }