CategoryDao.php 994 B

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