1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Comment; |
6
|
|
|
use AppBundle\Form\Type\CommentAdminType; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
11
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class CommentController |
17
|
|
|
* @package AppBundle\Controller\Admin |
18
|
|
|
* @Route("/admin") |
19
|
|
|
*/ |
20
|
|
|
class CommentController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param Request $request |
24
|
|
|
* @param $page |
25
|
|
|
* @Method({"GET", "POST"}) |
26
|
|
|
* @Route("/comments/{pager}/{page}", name="commentsAdmin", |
27
|
|
|
* defaults={"pager": "page", "page": 1}, |
28
|
|
|
* requirements={ |
29
|
|
|
* "pager": "page", |
30
|
|
|
* "page": "[1-9]\d*", |
31
|
|
|
* }) |
32
|
|
|
* @Template("AppBundle:admin:comments.html.twig") |
33
|
|
|
* |
34
|
|
|
* @return Response |
35
|
|
|
*/ |
36
|
|
|
public function roleAction(Request $request, $page = 1) |
37
|
|
|
{ |
38
|
|
|
$em = $this->getDoctrine()->getManager(); |
39
|
|
|
$comments = $em->getRepository("AppBundle:Comment") |
40
|
|
|
->getRecentComments($page, 10); |
41
|
|
|
|
42
|
|
|
$formData = $comments->getQuery()->getResult(); |
43
|
|
|
|
44
|
|
|
$form = $this->createFormBuilder($formData) |
|
|
|
|
45
|
|
|
->setAction($this->generateUrl('commentsAdmin')) |
46
|
|
|
->setMethod('POST') |
47
|
|
|
->add('comments', ChoiceType::class, array( |
48
|
|
|
'choices' => $formData, |
49
|
|
|
'choices_as_values' => true, |
50
|
|
|
'expanded' => true, |
51
|
|
|
'multiple' => true, |
52
|
|
|
'choice_value' => 'id', |
53
|
|
|
'label' => false, |
54
|
|
|
'choice_label' => 'id', |
55
|
|
|
) |
56
|
|
|
) |
57
|
|
|
->add('delete', SubmitType::class, array( |
58
|
|
|
'label' => 'Delete', |
59
|
|
|
'attr' => [ |
60
|
|
|
'class' => 'btn btn-xs btn-danger' |
61
|
|
|
], |
62
|
|
|
) |
63
|
|
|
) |
64
|
|
|
->getForm(); |
65
|
|
|
|
66
|
|
|
if ($request->getMethod() == 'POST') { |
67
|
|
|
$form->handleRequest($request); |
68
|
|
|
if ($form->isValid()) { |
69
|
|
|
$data = $form->getData(); |
70
|
|
|
foreach ($data['comments'] as $comment) { |
71
|
|
|
$em->remove($comment); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$em->flush(); |
76
|
|
|
} catch (\Exception $e) { |
77
|
|
|
return $this->render( |
78
|
|
|
'AppBundle:admin:failure.html.twig', |
79
|
|
|
array( |
80
|
|
|
'message' => 'Deleting record is failed. Because record has relation to other records or another reasons.', |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->redirectToRoute('commentsAdmin'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return [ |
90
|
|
|
'comments' => $comments, |
91
|
|
|
'delete' => $form->createView(), |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $id |
97
|
|
|
* @param $action |
98
|
|
|
* @param Request $request |
99
|
|
|
* @Route("/comment/{action}/{id}", name="commentEdit", |
100
|
|
|
* defaults={"id": 0}, |
101
|
|
|
* requirements={ |
102
|
|
|
* "action": "edit", |
103
|
|
|
* "id": "\d+" |
104
|
|
|
* }) |
105
|
|
|
* @Method({"GET", "POST"}) |
106
|
|
|
* @Template("AppBundle:admin/form:comment.html.twig") |
107
|
|
|
* |
108
|
|
|
* @return Response |
109
|
|
|
*/ |
110
|
|
|
public function editCommentAction($id, $action, Request $request) |
111
|
|
|
{ |
112
|
|
|
$em = $this->getDoctrine()->getManager(); |
113
|
|
|
$comment = $em->getRepository('AppBundle:Comment') |
114
|
|
|
->find($id); |
115
|
|
|
$title = 'Edit comment id: "'.$id. '" for article "' . $comment->getArticle()->getTitle() . '"'; |
116
|
|
|
|
117
|
|
|
$form = $this->createForm(CommentAdminType::class, $comment, [ |
118
|
|
|
'em' => $em, |
119
|
|
|
'action' => $this->generateUrl('commentEdit', ['action' => $action, 'id' => $id]), |
120
|
|
|
'method' => Request::METHOD_POST, |
121
|
|
|
]) |
122
|
|
|
->add('save', SubmitType::class, array('label' => 'Save')); |
123
|
|
|
|
124
|
|
View Code Duplication |
if ($request->getMethod() == 'POST') { |
|
|
|
|
125
|
|
|
$form->handleRequest($request); |
126
|
|
|
if ($form->isValid()) { |
127
|
|
|
$em->persist($comment); |
128
|
|
|
$em->flush(); |
129
|
|
|
|
130
|
|
|
return $this->redirectToRoute('commentsAdmin'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return [ |
135
|
|
|
'title' => $title, |
136
|
|
|
'form' => $form->createView(), |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: