Pārlūkot izejas kodu

ORM: using param converters, updating, deleting.

Frederic G. MARAND 6 gadi atpakaļ
vecāks
revīzija
7028984486

+ 12 - 0
public/styles/app.css

@@ -12,3 +12,15 @@ img.logo {
 .flash-error {
   background: #ce8483;
 }
+
+td {
+  border: thin solid orange;
+}
+
+td a {
+  display: block;
+}
+
+td.op {
+  text-align: center;
+}

+ 88 - 10
src/Controller/ProductController.php

@@ -3,19 +3,38 @@
 namespace App\Controller;
 
 use App\Entity\Product;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+use Doctrine\ORM\EntityManagerInterface;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\Routing\Annotation\Route;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Response;
 
 class ProductController extends Controller
 {
+
+  /**
+   * @Route(
+   *  name = "product_index",
+   *  path = "/product"
+   * )
+   *
+   * @param \Doctrine\ORM\EntityManagerInterface $em
+   *
+   * @return \Symfony\Component\HttpFoundation\Response
+   */
+  public function index(EntityManagerInterface $em) {
+    $repository = $em->getRepository(Product::class);
+    $products = $repository->findAll();
+    return $this->render('product/index.html.twig', ['products' => $products]);
+  }
+
   /**
    * @Route(
-   *   name="product",
-   *   path = "/product"
+   *   name="product_add",
+   *   path = "/product/add"
    * )
    */
-  public function index() {
+  public function add() {
     // EntityManager can also be injected as EntityManagerInterface $em.
     $em = $this->getDoctrine()->getManager();
 
@@ -35,16 +54,16 @@ class ProductController extends Controller
   }
 
   /**
-   * @\Symfony\Component\Routing\Annotation\Route(
-   *   name = "product_show",
-   *   path = "/product/{id}",
+   * @Route(
+   *   name = "product_show_manual",
+   *   path = "/product/{id}/manual",
    * )
    *
    * @param $id
    *
    * @return \Symfony\Component\HttpFoundation\Response
    */
-  public function show($id) {
+  public function showManually($id) {
     $repository = $this->getDoctrine()
       ->getRepository(Product::class);
 
@@ -74,8 +93,67 @@ class ProductController extends Controller
       );
     }
 
-    return $this->render('product.html.twig', [
-      'name' => $product->getName(),
+    return $this->render('product/product.html.twig', [
+      'product' => $product,
     ]);
   }
+
+  /**
+   * @Route(
+   *   name = "product_show",
+   *   path = "/product/{id}",
+   * )
+   *
+   * @param \App\Entity\Product $product
+   *
+   * @return \Symfony\Component\HttpFoundation\Response
+   */
+  public function show(Product $product) {
+    return $this->render('product/product.html.twig', [
+      'product' => $product,
+    ]);
+  }
+
+  /**
+   * @Route(
+   *   name = "product_raise",
+   *   path = "/product/{id}/raise"
+   * )
+   *
+   * @param \App\Entity\Product $product
+   * @param \Doctrine\ORM\EntityManagerInterface $em
+   *
+   * @return \Symfony\Component\HttpFoundation\RedirectResponse
+   */
+  public function update(Product $product, EntityManagerInterface $em) {
+    $price = $product->getPrice();
+    $price += 0.01;
+    $product->setPrice($price);
+    // $em->persist($product) is NOT needed but MAY be used as product is
+    // already mapped.
+    $em->flush();
+
+    $id = $product->getId();
+    $request = Request::create($this->generateUrl('product_show', ['id' => $id]));
+    $kernel = $this->get('http_kernel');
+    return $kernel->handle($request);
+  }
+
+  /**
+   * @Route(
+   *   name = "product_delete",
+   *   path = "/product/{id}/delete"
+   * )
+   *
+   * @param \App\Entity\Product $product
+   * @param \Doctrine\ORM\EntityManagerInterface $em
+   *
+   * @return \Symfony\Component\HttpFoundation\RedirectResponse
+   */
+  public function delete(Product $product, EntityManagerInterface $em) {
+    $em->remove($product);
+    $em->flush();
+
+    return $this->redirectToRoute('product_index');
+  }
 }

+ 0 - 7
templates/product.html.twig

@@ -1,7 +0,0 @@
-{% extends "base.html.twig" %}
-
-{% block title %}Product{% endblock %}
-
-{% block body %}
-    Check out this great product: {{ name }}
-{% endblock %}

+ 31 - 0
templates/product/index.html.twig

@@ -0,0 +1,31 @@
+{% extends "base.html.twig" %}
+
+{% block title %}Products{% endblock %}
+
+{% block body %}
+    Check out these great products:
+    <table>
+        <thead>
+            <tr>
+                <th>Id</th>
+                <th>Name</th>
+                <th>Price</th>
+                <th>Raise price</th>
+                <th>Delete</th>
+            </tr>
+        </thead>
+        <tbody>
+            {% for product in products %}
+                {% set idAttr = {'id': product.id} %}
+                {% set productLink = path('product_show', idAttr) %}
+            <tr>
+                <td><a href="{{ productLink }}">{{ product.id }}</a></td>
+                <td><a href="{{ productLink }}">{{ product.name }}</a></td>
+                <td>{{ "%10.2f"|format(product.price) }}</td>
+                <td class="op"><a href="{{ path('product_raise', idAttr) }}">+</a></td>
+                <td class="op"><a href="{{ path('product_delete', idAttr) }}">X</a></td>
+            </tr>
+            {% endfor %}
+        </tbody>
+    </table>
+{% endblock %}

+ 33 - 0
templates/product/product.html.twig

@@ -0,0 +1,33 @@
+{% extends "base.html.twig" %}
+
+{% block title %}Product{% endblock %}
+
+{% block body %}
+    Check out this great product:
+    <table>
+        <thead>
+            <tr>
+                <th>Attribute</th>
+                <th>Value</th>
+            </tr>
+        </thead>
+        <tbody>
+            <tr>
+                <td>Id</td>
+                <td>{{ product.id }}</td>
+            </tr>
+            <tr>
+                <td>Name</td>
+                <td>{{ product.name }}</td>
+            </tr>
+            <tr>
+                <td>Description</td>
+                <td>{{ product.description }}</td>
+            </tr>
+            <tr>
+                <td>Price</td>
+                <td>{{ '%10.2f'|format(product.price) }}</td>
+            </tr>
+        </tbody>
+    </table>
+{% endblock %}