Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3924

how could i add an entity in this class

$
0
0

hello, a friend tell me i coulld use handler and abstract cllass for handlle my symfony forms.

I'm new in symfony and try do llike he say,

some parts are ok, but it's not so easy

I had this three part code.

I use it here : create ==> it's ok

I want use the Handler in : show

The problem is i need send entity user and trick in my handler.

if you have an idea, i take and try it :)

thanks a lot

controller

<?php

namespace App\Controller;

use App\Entity\Comment;
use App\Entity\Trick;
use App\Form\CommentType;
use App\Form\TrickType;
use App\Handler\CommentHandler;
use App\Handler\TrickHandler;
use App\Repository\CommentRepository;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use App\Repository\TrickRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class TrickController extends AbstractController
{
    private $trickRepository;

    public function __construct(TrickRepository $trickRepository, CommentRepository $commentRepository)
    {
        $this->trickRepository = $trickRepository;
        $this->commentRepository = $commentRepository;

    }


    /**
     * @Route("/trick/{slug}", name="trick.show", requirements={"slug": "[a-z0-9\-]*"})
     * @return Response
     */
    public function show(Trick $trick, Request $request,CommentHandler $handler): Response
    {
        $comment = new Comment();
        $form = $this->createForm(CommentType::class, $comment)->handleRequest($request);
        $user = $this->getUser();
        $displayedComments = $this->commentRepository->getAllComments(1);

        if ($form->isSubmitted() && $form->isValid()) {
            $comment->setTrick($trick);
            $comment->setAuthor($user);
            $this->getDoctrine()->getManager()->persist($comment);
            $this->getDoctrine()->getManager()->flush();
            $slug = $trick->getSlug();

            return $this->redirectToRoute("trick.show",array('slug' => $slug));

        }
        return $this->render('pages/trick/show.html.twig',[
                'trick' => $trick,
                'displayedComments' => $displayedComments,
                'current_menu'=>'home',
                "form" => $form->createView()
            ]
        );
    }


    /**
     * @Route("/create", name="trick_create")
     * @param Request $request
     * @param TrickHandler $handler
     * @return Response
     */
    public function create(Request $request,TrickHandler $handler): Response
    {
        if($handler->handle($request, new Trick(), ["validation_groups" => ["Default", "add"]        ]
        )) {
            return $this->redirectToRoute("trick_create");
        }
        return $this->render("admin/trick/create.html.twig", [
            "form" => $handler->createView()
        ]);
    }
}

AbstractHandler

<?php

namespace App\Handler;


use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Test\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\FormFactoryInterface;

abstract class AbstractHandler
{
    /**
    * @var FormFactoryInterface
     */

    private $formFactory;

    /**
     * @var FormInterface
     */
    private $form;

    /**
     * @return string
     */
    abstract protected function getForm() : string;

    /**
     * @param $data
     * @param array $entities
     */
    abstract protected function process($data) : void;

    /**
     * @required
     * @param FormFactoryInterface $formFactory
     */
    public function setFormFactory(FormFactoryInterface $formFactory) :void
    {
        $this->formFactory = $formFactory;
    }

    /**
     * @param Request $request
     * @param $data
     * @param array $options
     * @return bool
     */
    public function handle(Request $request, $data, array $options = []) :bool
    {
        $this->form = $this->formFactory->create($this->getForm(), $data, $options)->handleRequest($request);
        if ($this->form->isSubmitted() && $this->form->isValid()) {
            $this->process($data);
            return true;
        }
        return false;
    }
    public function createView() :FormView
    {
        return $this->form->createView();
    }
}

CommentHandler

<?php
/**
 * Created by PhpStorm.
 * User: khysh
 * Date: 10/03/2020
 * Time: 19:13
 */

namespace App\Handler;

use App\Form\CommentType;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;

class CommentHandler extends AbstractHandler
{
    /**
     * @var EntityManagerInterface
     */
    private $entityManager;

    /**
     * TrickHandler constructor.
     * @param EntityManagerInterface $entityManager
     */
    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    protected function getForm(): string
    {
        // TODO: Implement getForm() method.
        return CommentType::class;
    }

    /**
     * @param $data
     * @param array $entities
     */
    protected function process($data): void
    {
        // TODO: Implement process() method.
        if ($this->entityManager->getUnitOfWork()->getEntityState($data) === UnitOfWork::STATE_NEW) {
            $this->entityManager->persist($data);
        }
        $this->entityManager->flush();

    }

}

Viewing all articles
Browse latest Browse all 3924

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>