Код: Выделить всё
{% if previousPageboolean == false %}
[url={{app.request.headers.get(]
[img]{{ asset([/img]
[/url]
{%else%}
[url={{referer}}]
[img]{{ asset([/img]
[/url]
{% endif %}
Контроллер
Код: Выделить всё
/**
* @Route("/receta/{title}", name="recipe_show", methods={"GET"})
* @Route("/receta/{title}", name="recipe_show")
*/
public function show(Recipe $recipe,RecipeRepository $recipeRepository,ScoreRepository $scoreRepository,CommentRepository $commentRepository, Request $request): Response
{
$comment = new Comment();
$score = new Score();
$comment_form = $this->createForm(CommentType::class, $comment);
$comment_form->handleRequest($request);
$score_form = $this->createForm(ScoreType::class, $score);
$score_form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$user = $this->getUser();
$referrer="";
$previousPageboolean =false;
if ($score_form->isSubmitted() && $score_form->isValid()) {
$previousPageboolean =true;
$referrer = $request->get('referrer');
$score_value = $score_form->get("score")->getData();
$isScore = $scoreRepository->findBy(array('user' => $user , 'recipe' => $recipe->getId()));
if($isScore == [] ){
$score->setScore($score_value);
$score->setRecipe($recipe);
$score->setUser($user);
$entityManager->persist($score);
$entityManager->flush();
$this->addFlash('success', '¡Puntuación registrada con exito!');
return $this->redirectToRoute($request->getUri(), [
'referrer' => $this->generateUrl($request->attributes->get('_route'),
array_merge(
$this->request->atrributes->get('_route_params'),
$this->request->query->all
),
),
]);
}else{
$this->addFlash('danger', 'Ya existe una puntuación registrada para esta receta con este usuario.');
return $this->redirect($request->getUri());
}
}
if ($comment_form->isSubmitted() && $comment_form->isValid()) {
$parentid = $comment_form->get("parent")->getData();
if($parentid != null){
$parent = $entityManager->getRepository(Comment::class)->find($parentid);
}
$comment->setVisible(1);
$comment->setParent($parent ?? null);
$comment->setUser($user);
$comment->setRecipe($recipe);
$comment->setCreatedAt(new DateTime());
$entityManager->persist($comment);
$entityManager->flush();
$this->addFlash('success', '¡Comentario registrado con exito!');
return $this->redirect($request->getUri());
}
$comments = $commentRepository->findCommentsByRecipe($recipe);
return $this->render('recipe/show/show.html.twig', [
'recipe' => $recipe,
'score_form' => $score_form->createView(),
'comment_form' => $comment_form->createView(),
'comments' => $comments,
'referrer' => $referrer,
'previousPageboolean' => $previousPageboolean
]);
}
Таблица рецептов не имеет свойства категории, поскольку у меня есть таблица рецептов_категории, которая имеет отношение M:M между таблицей рецептов и таблицей категорий.
Подробнее здесь: https://stackoverflow.com/questions/676 ... mfony-twig