save() method in PasteRepository implemented

This commit is contained in:
mitsuha_s 2023-07-21 17:13:05 +03:00
parent 78ebba425b
commit 561b016e38
2 changed files with 8 additions and 4 deletions

View file

@ -6,7 +6,6 @@ namespace App\Controller;
use App\Form\Type\PasteForm; use App\Form\Type\PasteForm;
use App\Entity\Paste; use App\Entity\Paste;
use App\Repository\PasteRepository; use App\Repository\PasteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -16,7 +15,7 @@ use Symfony\Component\HttpFoundation\Request;
class PasteController extends AbstractController class PasteController extends AbstractController
{ {
#[Route('/')] #[Route('/')]
public function new(Request $request, EntityManagerInterface $entityManager): Response { public function new(Request $request, PasteRepository $pasteRepository): Response {
$paste = new Paste(); $paste = new Paste();
$form = $this->createForm(PasteForm::class, $paste); $form = $this->createForm(PasteForm::class, $paste);
@ -30,8 +29,7 @@ class PasteController extends AbstractController
$paste->setSecret(hash('sha1', random_bytes(25))); $paste->setSecret(hash('sha1', random_bytes(25)));
} }
$entityManager->persist($paste); $pasteRepository->save($paste);
$entityManager->flush();
return $this->redirectToRoute($request->attributes->get('_route')); return $this->redirectToRoute($request->attributes->get('_route'));

View file

@ -12,4 +12,10 @@ class PasteRepository extends ServiceEntityRepository
public function __construct(ManagerRegistry $registry) { public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, Paste::class); parent::__construct($registry, Paste::class);
} }
public function save(Paste $paste): void {
$entityManager = $this->getEntityManager();
$entityManager->persist($paste);
$entityManager->flush();
}
} }