Bonjour,
Je souhaite afficher une liste de catégories comportant chacune leurs produits respectifs, dans ce style :
Le problème ici, c'est que la catégorie 2 ne comporte normalement que le produit 2 mais les 3 produits sont affiché sans leurs input text par contre ; voici le code qui génère cela :
Mon controller :
Mon repository :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 /** * @Route("/default_pricing") */ class DefaultPricingController extends Controller { /** * Displays a form to edit PricingV2 entity for each associated ProductParameters. * * @Route("/edit", name="default_pricing_edit") * @Method("GET") * @Template() * * @return array */ public function editAction() { $em = $this->getDoctrine()->getManager(); $products = $em->getRepository('Carvivo\CrmBundle\Entity\Billing\Product')->getAllProducts(); $pricings = $em->getRepository('Carvivo\CrmBundle\Entity\Billing\Pricing2')->findAll(); $categories = $em->getRepository('Carvivo\CrmBundle\Entity\Billing\BillingCategory')->findAll(); if (!$products) { throw $this->createNotFoundException('Unable to find Billing\Product entities.'); } $editForm = $this->createEditForm($pricings, $products); return [ 'edit_form' => $editForm->createView(), 'products' => $products, 'categories' => $categories ]; } /** * Creates a form to edit PricingV2 entity for an associated ProductParameters. * * @param array $products * * @return \Symfony\Component\Form\Form */ private function createEditForm($pricings, array $products) { $form = $this->createForm( new ProductType( $this->get('translator'), $products, Pricing2::STANDARD_RATE), $pricings, [ 'action' => $this->generateUrl('default_pricing_update'), 'method' => 'POST', ] ); $form->add('submit', SubmitType::class, array( 'label' => 'crm.controller.update', 'attr' => [ 'class' => 'button green'] )); return $form; } /** * Edits an existing PricingV2 entity for each associated ProductParameters. * * @param Request $request * @Route("/edit", name="default_pricing_update") * @Method("POST") * @Template("CarvivoCrmBundle:Billing\DefaultPricing:edit.html.twig") * * @return array|\Symfony\Component\HttpFoundation\RedirectResponse */ public function updateAction(Request $request) { $em = $this->getDoctrine()->getManager(); $products = $em->getRepository('Carvivo\CrmBundle\Entity\Billing\Product')->getAllProducts(); if (!$products) { throw $this->createNotFoundException('Unable to find Billing\Product entities.'); } $pricings = $em->getRepository('Carvivo\CrmBundle\Entity\Billing\Pricing2')->findAll(); $editForm = $this->createEditForm($pricings, $products); $editForm->handleRequest($request); foreach ($editForm->getData() as $product) { $em->persist($product); } if ($editForm->isSubmitted() && $editForm->isValid()) { $em->flush(); $this->get('session')->getFlashBag()->add('success', 'Products pricing updated.'); return $this->redirect($this->generateUrl('default_pricing_edit')); } return array( 'edit_form' => $editForm->createView() ); } }
Mon twig :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 public function getAllProducts() { return $this->createQueryBuilder('p') ->innerJoin('p.billingCategory', 'b') ->where('p.billingCategory = b.id') ->andWhere('p.isDiscount = 0') ->getQuery() ->getResult(); }
Je "triche" dans mon template pour ne pas afficher plusieurs fois les catégories en insérant le nom de la catégorie dans un tableau, je vérifie si celui existe pas, si c'est le cas j'affiche la catégorie qui suit.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 {% extends 'CarvivoCrmBundle:Admin:layout.html.twig' %} {% block title %}{{ parent() }} | Paramétrage des tarifs produits {% endblock %} {% block stylesheets %} {{ parent() }} {% stylesheets 'bundles/carvivocrm/css/components/submenu.css' 'bundles/carvivocrm/css/components/dataBlock.css' 'bundles/carvivocrm/css/components/panel.css' output='css/carvivocrm-billing-parameters.css' filter='cssrewrite,?yui_css' %} <link rel="stylesheet" type="text/css" media="all" href="{{ asset_url }}?v={{ asset_version }}" /> {% endstylesheets %} {% endblock %} {% block content %} <div class="columns cf editParameters pricingParameters"> {{ render(controller('CarvivoCrmBundle:Main:leftColumn', { 'pos': gdPosId|default(null) } )) }} <div class="right-column"> {% include 'CarvivoCrmBundle:Billing:_steps.html.twig' with {'step' : 'product_rate'} %} <div class="data cf"> <div class="header cf"> <h3>Paramétrage des tarifs produits</h3> </div> {% set array = [] %} {{ form_start(edit_form) }} <div class="content cf"> {% for category in categories %} <div class="c-data c-data--1-of-2"> <div class="c-data__title"> <h3>{{ category.name }}</h3> {% set array = array|merge([category.name]) %} </div> {% for productPricingType in edit_form %} {% if productPricingType.vars.value.product is defined %} {% if productPricingType.vars.value.product.billingcategory.name in array and productPricingType.vars.value.amount is defined %} {% set array = array|merge([productPricingType.vars.value.id]) %} {% if productPricingType.vars.value.id in array %} <div class="c-data c-data--1-of-2"> <div class="c-data__content"> <div class="c-panel"> {{ form_label(productPricingType.amount) }} {{ form_widget(productPricingType.amount) }} {{ form_errors(productPricingType.amount) }} </div> </div> </div> {% endif %} {% endif %} {% endif %} {% endfor %} </div> {% endfor %} {{ form_widget(edit_form.submit) }} </div> {{ form_widget(edit_form._token) }} {{ form_end(edit_form, {'render_rest': false}) }} </div> </div> </div> {% endblock %}
Je voulais faire la même chose pour les produits mais ça ne fonctionne pas.
Voici les liaisons de mes tables :
En sql, quand j'execute cette requête :
J'ai ce résultat :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 SELECT crm_billing_category.name, crm_product.name, crm_pricing_2.amount FROM `crm_pricing_2` JOIN `crm_product` ON `crm_pricing_2`.`product_id` = `crm_product`.`id` JOIN `crm_billing_category` ON `crm_product`.`billing_category_id` = `crm_billing_category`.`id` GROUP BY crm_product.name ORDER BY `crm_billing_category`.`name` ASC
C'est cette requête que je devrais inclure dans mon repository ?
Merci,
Partager