TP4 ORM Doctrine Et Opérations CRUD
TP4 ORM Doctrine Et Opérations CRUD
TP4 ORM Doctrine Et Opérations CRUD
Pour réaliser les différentes opération CRUD, on va utiliser dans cet atelier
L’ORM Doctrine (ORM : Object Relational Mapping) qui va prendre en
charge la correspondance entre les objets PHP et les tables de la BD MySql
<?php
namespace App\Entity;
/**
* @ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
*/
class Article
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $nom;
/**
* @ORM\Column(type="decimal", precision=10, scale=0)
*/
private $prix;
return $this;
}
return $this;
}
/**
* @Route("/article/save")
*/
public function save() {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($article);
$entityManager->flush();
use App\Entity\Article;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
/**
*@Route("/",name="article_list")
*/
public function home()
{
//récupérer tous les articles de la table article de la BD
// et les mettre dans le tableau $articles
$articles= $this->getDoctrine()->getRepository(Article::class)->findAll();
return $this->render('articles/index.html.twig',['articles'=> $articles]);
}
{% extends 'base.html.twig' %}
{% block title%} Liste des Articles{% endblock %}
{% block body %}
{% if articles %}
<table id="articles" class="table table-striped">
<thead>
<tr>
<th>Nom</th>
<th>Prix</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for article in articles %}
<tr>
<td>{{ article.nom }}</td>
<td>{{ article.prix }}</td>
<td>
<a href="/article/{{ article.id }}" class="btn btn-dark">Détails</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>Aucun articles</p>
{% endif %}
{% endblock %}
/**
* @Route("/article/new", name="new_article")
* Method({"GET", "POST"})
*/
public function new(Request $request) {
$article = new Article();
$form = $this->createFormBuilder($article)
->add('nom', TextType::class)
->add('prix', TextType::class)
->add('save', SubmitType::class, array(
$form->handleRequest($request);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($article);
$entityManager->flush();
return $this->redirectToRoute('article_list');
}
return $this->render('articles/new.html.twig',['form' => $form-
>createView()]);
}
{% extends 'base.html.twig' %}
/**
* @Route("/article/{id}", name="article_show")
*/
public function show($id) {
$article = $this->getDoctrine()->getRepository(Article::class)
->find($id);
return $this->render('articles/show.html.twig',
array('article' => $article));
}
{% extends 'base.html.twig' %}
{% block title %}{{ article.nom }}{% endblock %}
{% block body %}
<h1>{{ article.nom }}</h1>
<p>{{ article.prix }}</p>
<hr>
<a href="/">Retour</a>
{% endblock %}
Modifier un article
22. Ajouter, au contrôleur indexController.php, la route et la fonction qui
permettent de modifier un article :
/**
* @Route("/article/edit/{id}", name="edit_article")
* Method({"GET", "POST"})
*/
public function edit(Request $request, $id) {
$article = new Article();
$article = $this->getDoctrine()->getRepository(Article::class)->find($id);
$form = $this->createFormBuilder($article)
->add('nom', TextType::class)
->add('prix', TextType::class)
->add('save', SubmitType::class, array(
'label' => 'Modifier'
))->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
return $this->redirectToRoute('article_list');
}
{% extends 'base.html.twig' %}
{% block body %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endblock %}
<td>
<a href="/article/{{ article.id }}" class="btn btn-dark">Détails</a>
<a href="/article/edit/{{ article.id }}" class="btn btn-dark">Modifier</a>
</td>
Supprimer un article
25. Modifier le fichier index.htm.twig pour ajouter le bouton Supprimer :
<td>
<a href="/article/{{ article.id }}" class="btn btn-dark">Détails</a>
<a href="/article/edit/{{ article.id }}" class="btn btn-
dark">Modifier</a>
<a href="/article/delete/{{ article.id }}" class="btn btn-danger"
onclick="return confirm('Etes-
vous sûr de supprimer cet article?');">Supprimer</a>
</td>
/**
* @Route("/article/delete/{id}",name="delete_article")
* @Method({"DELETE"})
*/
public function delete(Request $request, $id) {
$article = $this->getDoctrine()->getRepository(Article::class)-
>find($id);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($article);
$entityManager->flush();
return $this->redirectToRoute('article_list');
}