adding Beer entity, migration and crud

This commit is contained in:
Tchama 2021-02-18 20:13:14 +01:00
parent e1d462311f
commit 0fdd876bc4
11 changed files with 362 additions and 0 deletions

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210218191150 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SEQUENCE beer_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE beer (id INT NOT NULL, name VARCHAR(255) NOT NULL, alcool DOUBLE PRECISION DEFAULT NULL, PRIMARY KEY(id))');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('DROP SEQUENCE beer_id_seq CASCADE');
$this->addSql('DROP TABLE beer');
}
}

View File

@ -0,0 +1,94 @@
<?php
namespace App\Controller;
use App\Entity\Beer;
use App\Form\BeerType;
use App\Repository\BeerRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/beer")
*/
class BeerController extends AbstractController
{
/**
* @Route("/", name="beer_index", methods={"GET"})
*/
public function index(BeerRepository $beerRepository): Response
{
return $this->render('beer/index.html.twig', [
'beers' => $beerRepository->findAll(),
]);
}
/**
* @Route("/new", name="beer_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$beer = new Beer();
$form = $this->createForm(BeerType::class, $beer);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($beer);
$entityManager->flush();
return $this->redirectToRoute('beer_index');
}
return $this->render('beer/new.html.twig', [
'beer' => $beer,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="beer_show", methods={"GET"})
*/
public function show(Beer $beer): Response
{
return $this->render('beer/show.html.twig', [
'beer' => $beer,
]);
}
/**
* @Route("/{id}/edit", name="beer_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Beer $beer): Response
{
$form = $this->createForm(BeerType::class, $beer);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('beer_index');
}
return $this->render('beer/edit.html.twig', [
'beer' => $beer,
'form' => $form->createView(),
]);
}
/**
* @Route("/{id}", name="beer_delete", methods={"DELETE"})
*/
public function delete(Request $request, Beer $beer): Response
{
if ($this->isCsrfTokenValid('delete'.$beer->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($beer);
$entityManager->flush();
}
return $this->redirectToRoute('beer_index');
}
}

58
app/src/Entity/Beer.php Normal file
View File

@ -0,0 +1,58 @@
<?php
namespace App\Entity;
use App\Repository\BeerRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=BeerRepository::class)
*/
class Beer
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="float", nullable=true)
*/
private $alcool;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getAlcool(): ?float
{
return $this->alcool;
}
public function setAlcool(?float $alcool): self
{
$this->alcool = $alcool;
return $this;
}
}

26
app/src/Form/BeerType.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace App\Form;
use App\Entity\Beer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BeerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('alcool')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Beer::class,
]);
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Beer;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Beer|null find($id, $lockMode = null, $lockVersion = null)
* @method Beer|null findOneBy(array $criteria, array $orderBy = null)
* @method Beer[] findAll()
* @method Beer[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class BeerRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Beer::class);
}
// /**
// * @return Beer[] Returns an array of Beer objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('b')
->andWhere('b.exampleField = :val')
->setParameter('val', $value)
->orderBy('b.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Beer
{
return $this->createQueryBuilder('b')
->andWhere('b.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@ -0,0 +1,5 @@
<form method="post" action="{{ path('beer_delete', {'id': beer.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ beer.id) }}">
<button class="btn">Delete</button>
</form>

View File

@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

View File

@ -0,0 +1,13 @@
{% extends 'base.html.twig' %}
{% block title %}Edit Beer{% endblock %}
{% block body %}
<h1>Edit Beer</h1>
{{ include('beer/_form.html.twig', {'button_label': 'Update'}) }}
<a href="{{ path('beer_index') }}">back to list</a>
{{ include('beer/_delete_form.html.twig') }}
{% endblock %}

View File

@ -0,0 +1,37 @@
{% extends 'base.html.twig' %}
{% block title %}Beer index{% endblock %}
{% block body %}
<h1>Beer index</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Alcool</th>
<th>actions</th>
</tr>
</thead>
<tbody>
{% for beer in beers %}
<tr>
<td>{{ beer.id }}</td>
<td>{{ beer.name }}</td>
<td>{{ beer.alcool }}</td>
<td>
<a href="{{ path('beer_show', {'id': beer.id}) }}">show</a>
<a href="{{ path('beer_edit', {'id': beer.id}) }}">edit</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="4">no records found</td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="{{ path('beer_new') }}">Create new</a>
{% endblock %}

View File

@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}
{% block title %}New Beer{% endblock %}
{% block body %}
<h1>Create new Beer</h1>
{{ include('beer/_form.html.twig') }}
<a href="{{ path('beer_index') }}">back to list</a>
{% endblock %}

View File

@ -0,0 +1,30 @@
{% extends 'base.html.twig' %}
{% block title %}Beer{% endblock %}
{% block body %}
<h1>Beer</h1>
<table class="table">
<tbody>
<tr>
<th>Id</th>
<td>{{ beer.id }}</td>
</tr>
<tr>
<th>Name</th>
<td>{{ beer.name }}</td>
</tr>
<tr>
<th>Alcool</th>
<td>{{ beer.alcool }}</td>
</tr>
</tbody>
</table>
<a href="{{ path('beer_index') }}">back to list</a>
<a href="{{ path('beer_edit', {'id': beer.id}) }}">edit</a>
{{ include('beer/_delete_form.html.twig') }}
{% endblock %}