· 8 years ago · Feb 12, 2018, 09:58 AM
1/**
2 * Agent
3 *
4 * @ORMTable(name="agent")
5 * @ORMEntity(repositoryClass="......BundleRepositoryAgentRepository")
6 */
7class Agent
8{
9 /**
10 * @var integer
11 *
12 * @ORMId
13 * @ORMColumn(name="age_id", type="integer")
14 * @ORMGeneratedValue(strategy="IDENTITY")
15 */
16 private $id;
17
18 /**
19 * @var string
20 *
21 * @ORMColumn(name="age_numero", type="string", length=5, nullable=true)
22 */
23
24 private $ageNumero;
25
26 /**
27 * @var string
28 *
29 * @ORMColumn(name="age_nom", type="string", length=100, nullable=true)
30 */
31
32 private $ageNom;
33
34 /**
35 * @var string
36 *
37 * @ORMColumn(name="age_prenom", type="string", length=100, nullable=true)
38 */
39 private $agePrenom;
40
41 /**
42 * @var ArrayCollection
43 *
44 * @ORMManyToMany(targetEntity="Habilitation", inversedBy="agents")
45 * @ORMJoinTable(
46 * name="agent_j_habilitation",
47 * joinColumns={@ORMJoinColumn(name="fk_hab_id", referencedColumnName="id", nullable=false)},
48 * inverseJoinColumns={@ORMJoinColumn(name="fk_age_id", referencedColumnName="id", nullable=false)}
49 * )
50 *
51 */
52 private $habilitations;
53
54 /**
55 * Agent constructor.
56 */
57 public function __construct()
58 {
59 $this->habilitations = new ArrayCollection();
60 }
61
62 /**
63 * @param Habilitation $habilitation
64 */
65 public function addHabilitation(Habilitation $habilitation)
66 {
67 $habilitation->addAgent($this);
68 $this->habilitations[] = $habilitation;
69 }
70}
71
72/**
73 * Habilitation
74 *
75 * @ORMTable(name="habilitation")
76 * @ORMEntity
77 * @ORMEntity(repositoryClass="......BundleRepositoryHabilitationRepository")
78 */
79class Habilitation
80{
81 /**
82 * @var integer
83 *
84 * @ORMId
85 * @ORMColumn(name="hab_id", type="integer")
86 * @ORMGeneratedValue(strategy="IDENTITY")
87 */
88 private $id;
89
90 /**
91 * @var string
92 *
93 * @ORMColumn(name="hab_libelle", type="string", length=50, nullable=true)
94 */
95 private $habLibelle;
96
97 /**
98 * @var ArrayCollection
99 *
100 * @ORMManyToMany(targetEntity="Agent", mappedBy="habilitations")
101 */
102 private $agents;
103
104 /**
105 * Habilitation constructor.
106 */
107 public function __construct()
108 {
109 $this->agents = new ArrayCollection();
110 }
111
112 /**
113 * @param Agent $agent
114 */
115 public function addAgent(Agent $agent)
116 {
117 $this->agents[] = $agent;
118 }
119}
120
121class AjoutUserType extends AbstractType
122{
123 /**
124 * Constructor.
125 *
126 */
127 public function __construct()
128 {
129 }
130
131 /**
132 * @param FormBuilderInterface $builder
133 * @param array $options
134 */
135 public function buildForm(FormBuilderInterface $builder, array $options)
136 {
137
138 $builder
139 ->add('ageNumero', 'text', [
140 'label' => 'Numéro'
141 ])
142 ->add('ageNom', 'text', [
143 'label' => 'Nom'
144 ])
145 ->add('agePrenom', 'text', [
146 'label' => 'Prénom'
147 ])
148 ->add('habilitations', EntityType::class, [
149 'class' => 'RAVCBundle:Habilitation',
150 'label' => 'Habilitation',
151 'property' => 'habLibelle',
152 'multiple' => true,
153 'placeholder' => '-- Sélectionnez une habilitation --',
154 ]);
155 }
156
157
158
159 /**
160 * @param OptionsResolver $resolver
161 */
162 public function configureOptions(OptionsResolver $resolver)
163 {
164 $resolver->setDefaults(
165 [
166 'data_class' => Agent::class
167 ]
168 );
169 }
170}
171
172/**
173 * @Route("/ajoutUser", name="ravc_ajoutUser")
174 * @Method({"GET", "POST"})
175 * @internal param Request $request
176 */
177 public function newAgentAction(Request $request)
178 {
179 $agent = new Agent();
180
181 $form = $this->createForm(new AjoutUserType(), $agent);
182
183 $form->handleRequest($request);
184 if ($form->isSubmitted() && $form->isValid()) {
185 $em = $this->getDoctrine()->getManager();
186 $em->persist($agent);
187 $em->flush();
188 $this->notification('L'agent a été ajouté avec succès.');
189
190 return $this->redirectToRoute('ravc_ajoutUser');
191 }
192
193 return $this->render('RAVCBundle:Admin:ajoutUser.html.twig', ['form' => $form->createView()]);
194 }
195
196CREATE TABLE `agent` (
197 `age_id` int(11) NOT NULL AUTO_INCREMENT,
198 `age_numero` varchar(6) DEFAULT NULL,
199 `age_nom` varchar(100) DEFAULT NULL,
200 `age_prenom` varchar(100) DEFAULT NULL,
201 PRIMARY KEY (`age_id`)
202) ENGINE=InnoDB AUTO_INCREMENT=91 DEFAULT CHARSET=latin1;
203
204CREATE TABLE `agent_j_habilitation` (
205 `ajh_id` int(11) NOT NULL AUTO_INCREMENT,
206 `fk_hab_id` int(11) NOT NULL,
207 `fk_age_id` int(11) NOT NULL,
208 PRIMARY KEY (`ajh_id`),
209 KEY `ajh_fk_hab_id` (`fk_hab_id`),
210 KEY `ajh_fk_age_id` (`fk_age_id`),
211 CONSTRAINT `ajh_fk_age_id` FOREIGN KEY (`fk_age_id`) REFERENCES `agent` (`age_id`) ON DELETE CASCADE ON UPDATE CASCADE,
212 CONSTRAINT `ajh_fk_hab_id` FOREIGN KEY (`fk_hab_id`) REFERENCES `habilitation` (`hab_id`) ON DELETE CASCADE ON UPDATE CASCADE
213) ENGINE=InnoDB AUTO_INCREMENT=109 DEFAULT CHARSET=latin1;
214
215-- ----------------------------
216-- Table structure for habilitation
217-- ----------------------------
218DROP TABLE IF EXISTS `habilitation`;
219CREATE TABLE `habilitation` (
220 `hab_id` int(11) NOT NULL AUTO_INCREMENT,
221 `hab_libelle` varchar(50) DEFAULT NULL,
222 PRIMARY KEY (`hab_id`)
223) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;