Bonjour, voici mon problème actuel sur un projet de création d'un forum via Symfony 4.2.
Comme vous pouvez le voir sur l'image ci-dessous, je réussi à afficher le nom de la category (Fantasy) et tous les topics dans la liste en dessous sous forme d'un tableau afin d'avoir le titre, le nom de l'auteur du topic, la différence de la date etc..
Voici le code qui me permet cet affichage :
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 // App\Controller\CategoriesController.php public function show($id): Response { $category = $this->getDoctrine() ->getRepository(Categories::class) ->find($id); $topics = $category->getTopics(); return $this->render('pages/showCategory.html.twig', [ 'category' => $category, 'topics' => $topics, ]); }
Code Twig : 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 {% extends "base.html.twig" %} {% block title %} {{ category.catName }} {% endblock %} {% block body %} <div class="section"> <div class="sect-title">{{ category.catName }}</div> <div class="sect-child"> <table> <tr> <td>Topics</td> <td>Answers</td> <td>Views</td> <td>Last post</td> </tr> {% for topic in topics %} <tr> <td class="sect-child-title"> <a href="#">{{ topic.topicsubject }}</a> <br>by , {{ topic.topicdate|time_diff }} </td> <td> nb <!-- answers --> </td> <td> nb <!-- views --> </td> <td> datetime <br> by user </td> </tr> {% endfor %} </table> </div> </div> {% endblock %}
Je n'arrive pas à récupérer l'username qui se trouve dans l'Entity User afin d'ensuite le rajouter dans mon fichier template Twig.
J'ai essayé d'ajouter dans la fonction ci-dessus des morceaux de code qui pouvaient fonctionner (selon ma logique ) mais sans résultat.
Celui-ci m'a donné le nom de tous les utilisateurs qui ont créé un topic (dans l'ordre logique le 1er = user1, le 2nd = user2 et le 3eme = user3 dans la bdd) :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 $user = $this->getDoctrine() ->getRepository(User::class) ->findAll(); return $this->render('pages/showCategory.html.twig', [ 'category' => $category, 'topics' => $topics, 'user' => $user, ]);
Code Twig : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <td class="sect-child-title"> <a href="#">{{ topic.topicsubject }}</a> <br>by {% for user in user %} by {{ user.username }} {% endfor %} , {{ topic.topicdate|time_diff }} </td>
Vous vous en doutez il me retourne tous les noms via la boucle for..
Pour aller plus loin, l'Entity Topics possede un topic_by lié en ManyToOne avec l'id de l'Entity User et topic_cat lié avec l'id de l'Entity Categories.
Merci !
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 //App\Entity\Topics.php <?php namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\TopicsRepository") */ class Topics { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $topic_subject; /** * @ORM\Column(type="datetime") */ private $topic_date; /** * @ORM\ManyToOne(targetEntity="App\Entity\Categories", inversedBy="topics") * @ORM\JoinColumn(nullable=false) */ private $topic_cat; /** * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="topics") * @ORM\JoinColumn(nullable=false) */ private $topic_by; /** * @ORM\OneToMany(targetEntity="App\Entity\Replies", mappedBy="reply_topic") */ private $replies; public function __construct() { $this->replies = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getTopicSubject(): ?string { return $this->topic_subject; } public function setTopicSubject(string $topic_subject): self { $this->topic_subject = $topic_subject; return $this; } public function getSlug(): string { return (new Slugify())->slugify($this->topic_subject); } public function getTopicDate(): ?\DateTimeInterface { return $this->topic_date; } public function setTopicDate(\DateTimeInterface $topic_date): self { $this->topic_date = $topic_date; return $this; } public function getTopicCat(): ?Categories { return $this->topic_cat; } public function setTopicCat(?Categories $topic_cat): self { $this->topic_cat = $topic_cat; return $this; } public function getTopicBy(): ?User { return $this->topic_by; } public function setTopicBy(?User $topic_by): self { $this->topic_by = $topic_by; return $this; } /** * @return Collection|Replies[] */ public function getReplies(): Collection { return $this->replies; } public function addReply(Replies $reply): self { if (!$this->replies->contains($reply)) { $this->replies[] = $reply; $reply->setReplyTopic($this); } return $this; } public function removeReply(Replies $reply): self { if ($this->replies->contains($reply)) { $this->replies->removeElement($reply); // set the owning side to null (unless already changed) if ($reply->getReplyTopic() === $this) { $reply->setReplyTopic(null); } } return $this; } }
Partager