0% found this document useful (0 votes)
15 views2 pages

3

This PHP code defines a TransactionController class with methods to handle CRUD operations for Transaction entities using Doctrine ORM and Symfony forms. Methods include index, new, edit, and delete to display, create, update and remove Transaction records from the database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

3

This PHP code defines a TransactionController class with methods to handle CRUD operations for Transaction entities using Doctrine ORM and Symfony forms. Methods include index, new, edit, and delete to display, create, update and remove Transaction records from the database.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

<?

php
// src/Controller/TransactionController.php

namespace App\Controller;

use App\Entity\Transaction;
use App\Form\TransactionType;
use App\Repository\TransactionRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route("/transaction")
*/
class TransactionController extends AbstractController
{
/**
* @Route("/", name="transaction_index", methods={"GET"})
*/
public function index(TransactionRepository $transactionRepository): Response
{
$transactions = $transactionRepository->findAll();

return $this->render('transaction/index.html.twig', [
'transactions' => $transactions,
]);
}

/**
* @Route("/new", name="transaction_new", methods={"GET","POST"})
*/
public function new(Request $request, EntityManagerInterface $entityManager):
Response
{
$transaction = new Transaction();
$form = $this->createForm(TransactionType::class, $transaction);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {


$entityManager->persist($transaction);
$entityManager->flush();

return $this->redirectToRoute('transaction_index');
}

return $this->render('transaction/new.html.twig', [
'transaction' => $transaction,
'form' => $form->createView(),
]);
}

/**
* @Route("/{id}/edit", name="transaction_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Transaction $transaction,
EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(TransactionType::class, $transaction);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {


$entityManager->flush();

return $this->redirectToRoute('transaction_index');
}

return $this->render('transaction/edit.html.twig', [
'transaction' => $transaction,
'form' => $form->createView(),
]);
}

/**
* @Route("/{id}", name="transaction_delete", methods={"POST"})
*/
public function delete(Request $request, Transaction $transaction,
EntityManagerInterface $entityManager): Response
{
if ($this->isCsrfTokenValid('delete'.$transaction->getId(), $request-
>request->get('_token'))) {
$entityManager->remove($transaction);
$entityManager->flush();
}

return $this->redirectToRoute('transaction_index');
}
}

You might also like