src/Controller/CommentsController.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Product;
  4. use App\Entity\Comments;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. class CommentsController extends AbstractController
  10. {
  11.     public function __construct(EntityManagerInterface $entityManager){
  12.         $this->entityManager $entityManager;
  13.      }
  14.      
  15.     #[Route('/comments'name'app_comments')]
  16.     public function index(): Response
  17.     {
  18.         $activity $this->entityManager->getRepository(Product::class)->findAll();
  19.         $products $this->entityManager->getRepository(Product::class)->findAll();
  20.         $comments $this->entityManager->getRepository(Comments::class)->findAll();
  21.         $commentsRepository $this->entityManager->getRepository(Comments::class);
  22.         $commentsByProduct = array();
  23.         foreach ($products as $product) {
  24.             $idProduct $product->getId();
  25.             $commentsId = array();
  26.             $i 1;
  27.             foreach($comments as $comment) {
  28.                 if($comment->getActivity()->getId() == $idProduct && $comment->isIsValid()) {
  29.                     array_push($commentsId, [$i++, $comment]);
  30.                 }
  31.             }
  32.             array_push($commentsByProduct, [$idProduct$commentsId1]);
  33.         }
  34.         return $this->render('comments/index.html.twig', [
  35.             'products' => $products,
  36.             'commentsByProduct' => $commentsByProduct,
  37.             'comments' => $comments,
  38.             'activity' => $activity
  39.         ]);
  40.     }
  41. }