Bonjour je me forme sur Php via cakePHP pour un projet pro.
Je ne suis qu'au tout veuillez excusez ma potentielle ignorance.
L'idée de mon contrôleur est tout simple. Je récupère des centrales d'achat auquel des magasins sont rattaché.
J'ai déjà réussi a récupérer l'ensemble des centrales et de compter le nombre de magasins totale.
l'idée suivante était de récupérer le nombre de magasins rattaché a chaque centrale mais la j'avoue en parcourant la documentation ne pas avoir la bonne syntaxe.
Voici mon contrôleur :
Je pense que ma boucle a forcement un souci bien que pour moi elle me semble logique.
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 <?php //src/Controller/CentralesController.php namespace App\Controller; use Cake\ORM\TableRegistry; class CentralesController extends AppController { public function index() { //Recuperation de l'ensemble des centrale $centrales = $this->Centrales->find('all'); $this->set(compact('centrales')); //Nombre De Centrale $Nombrecentrales = $centrales -> count (); $this->set(compact('Nombrecentrales')); //Nombre De Magasin $magasins = TableRegistry::get('Magasins'); $magasins = TableRegistry::getTableLocator()->get('Magasins'); $magasins = $magasins ->find('all'); //Nombre de magasins totale $NombreMagasins = $magasins ->count (); $this->set(compact('NombreMagasins')); //Nombre de magasins par centrale foreach ($centrales as $centrale); $NombreMagasinscentrale = $magasins -> count( [ 'conditions => [magasins.centrale LIKE => $centrales' ] ); $this->set(compact('NombreMagasinscentrale')); } public function view($id = null) { $Centrale = $this->Centrales->get($id); $this->set(compact('centrale')); } public function add() { } }
Après réflexion même si j'arrivais à transmettre un résultat cohérent je ne vois pas comment je pourrai le transmettre dans ma vue de façon ordonné.
Code html : 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 <div class="container"> <table class="table"> <tr> <td> Nombre de centrales : <?= $Nombrecentrales ?><td> <td> Nombre de Magasins : <?= $NombreMagasins ?><td> <td><td> <td><a class="btn btn-primary" href="/Centrales/add" role="button">Ajouter Centrale</a></td> </tr> </table> <table class="table"> <tr> <th scope="row">ID</th> <th scope="row">Raison Sociale</th> <th scope="row">Ville</th> <th scope="row">Nombre de Magasin</th> <th scope="row">Detail</th> </tr> <?php foreach ($centrales as $centrale): ?> <tr> <th scope="row"><?= $centrale->id ?></th> <th scope="row"><?= $centrale->Raison_Sociale ?></th> <th scope="row"><?= $centrale->Ville ?></th> <th scope="row">XXXX Magasins</th> <td><a class="btn btn-primary" <?= $this->Html->link("DETAIL", ['action' => 'view', $centrale->id]) ?> </a></td> </tr> <?php endforeach; ?> </table> </div>
Merci de votre retour.
Partager