Académique Documents
Professionnel Documents
Culture Documents
Doctrine
https://symfony.com/doc/current/doctrine.html
Doctrine
1. Mapping ORM
2. Installation et configuration Doctrine
3. Création de tables
4. Relations et associations
5. Lecture et écriture en base de donnée
6. Mise à jour et suppression
7. Requêtes dynamiques avec Query Builder
Mapping ORM
object-relational mapping
mapping objet-relationnel
Objet
https://www.doctrine-project.org
Mapping ORM
Transforme notre modèle en SQL
<?php
/** @Entity */
class Message
{
/** @Column(type="integer") */
private $id;
/** @Column(length=140) */
private $text;
/** @Column(type="datetime", name="posted_at") */
private $postedAt;
}
Installation et configuration
composer require symfony/orm-pack
http://127.0.0.1:8000/article
Lecture et écriture en base de
données
Nous allons maintenant écrire dans notre base de données avant
d’afficher notre page
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($article);
$entityManager->flush();
return $this->render('article/index.html.twig', [
'article' => $article,
]);
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="article")
* @ORM\JoinColumn(nullable=false)
*/
private $category;
A vous
$category = $this->getDoctrine()
->getRepository(Category::class)
->find($id);
• Ajoutez des liens pour naviguer entre ces pages (cf : cours twig)
Lecture en BDD automatique
Avant Après
/**
* @Route("/category/{id}", name="category_show")
*/
public function show($id)
{
$category = $this->getDoctrine()
->getRepository(Category::class) /**
->find($id); * @Route("/category/{category}", name="category_show")
*/
public function show(Category $category)
{
if (!$category) {
return $this->render('category/index.html.twig', [
throw $this->createNotFoundException( 'category' => $category,
'No category found for id '.$id ]);
);
} }
return $this->render('category/index.html.twig', [
'category' => $category,
]);
/**
* @ORM\OneToMany(targetEntity="App\Entity\Article",
mappedBy="category")
*/
private $article;
/**
* @Route("/article/{article}/update",
name="article_update")
*/
public function update(Article $article)
{
$article->setTitre('Mon titre mis a jour');
$this->getDoctrine()->getManager()-
>flush();
Suppression d’un objet en BDD
/**
* @Route("/article/{article}/delete",
name="article_delete")
*/
public function delete(Article $article){
$entityManager = $this->getDoctrine()-
>getManager();
$entityManager-
>remove($article);
$entityManager->flush();
Requêtes dynamiques avec
QueryBuilder
Permet de faire des requêtes plus complexes en utilisant notre repository
{% block body %}
{{ nbArticle['nbArticle'] }} articles sur notre forum
{% endblock %}
index.html.twig
Exemple :
Exemple 2: On affiche les articles ayant un mot envoyé en paramètre dans leur titre
/**
* @Route("/article/search/{string}",
name="article_delete")
*/
public function search($string){
$articleRepository = $this->getDoctrine()-
>getRepository(Article::class);
$articles = $articleRepository- ArticleController.php
>search($string);
return $this->render('article/search.html.twig',
[ 'articles' => $articles
]);
}
Exemple :
Exemple 2: On affiche les articles ayant un mot envoyé en paramètre dans leur titre
{% extends 'base.html.twig' %}
search.html.twig
{% block title %}Search ArticleController!{% endblock %}
{% block body %}
{% for art in articles %}
{{ art.titre }} <br>
{% endfor %}
{% endblock %}
A vous
- Ajoutez sur les pages catégories et articles un lien pour supprimer les
articles et les catégories
Attention : Vous ne pouvez pas supprimer une catégorie contenant des articles