Bonjour, je souhaite intégrer ce code dans mon projet symfony pour que la référence de mon vol soit automatiquement généré quand je valide mon formulaire avec le reste des informations du vol.
Voici le bout de code que je souhaite insérer :
Voici le code dans lequel je souhaiterai insérer le code ci dessus :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 <?php function generate_reference() { return chr(mt_rand(ord('A'), ord('Z'))) . chr(mt_rand(ord('A'), ord('Z'))) . sprintf('%03d', mt_rand(0, 999)); } for ($i = 0; $i < 100; $i++) { echo generate_reference(), "\r\n"; }
Controller :
Entity :
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 #[Route('/new', name: 'app_flights_new', methods: ['GET', 'POST'])] public function new(Request $request, FlightsRepository $flightsRepository): Response { $flight = new Flights(); $form = $this->createForm(FlightsType::class, $flight); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $flightsRepository->add($flight, true); $this->addFlash('success', 'Your flight has been published'); return $this->redirectToRoute('app_flights_index', [], Response::HTTP_SEE_OTHER); } return $this->renderForm('flights/new.html.twig', [ 'flight' => $flight, 'form' => $form, ]); }
Form :
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 <?php namespace App\Entity; use App\Repository\FlightsRepository; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: FlightsRepository::class)] class Flights { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private $id; #[ORM\Column(type: 'string', length: 255)] private $flight_number; #[ORM\Column(type: 'date')] private $day; #[ORM\Column(type: 'time')] private $departure_time; #[ORM\Column(type: 'date')] private $day_arrival; #[ORM\Column(type: 'time')] private $arrival_time; #[ORM\Column(type: 'integer')] private $price; #[ORM\Column(type: 'boolean')] private $discount; #[ORM\Column(type: 'integer')] private $number_place; #[ORM\ManyToOne(targetEntity: DepartureCity::class, inversedBy: 'flights')] #[ORM\JoinColumn(nullable: false)] private $departureCity; #[ORM\ManyToOne(targetEntity: ArrivalCity::class, inversedBy: 'flights')] #[ORM\JoinColumn(nullable: false)] private $arrivalCity; public function getId(): ?int { return $this->id; } public function getFlightNumber(): ?string { return $this->flight_number; } public function setFlightNumber(string $flight_number): self { $this->flight_number = $flight_number; return $this; } public function getDay(): ?\DateTimeInterface { return $this->day; } public function setDay(\DateTimeInterface $day): self { $this->day = $day; return $this; } public function getDepartureTime(): ?\DateTimeInterface { return $this->departure_time; } public function setDepartureTime(\DateTimeInterface $departure_time): self { $this->departure_time = $departure_time; return $this; } public function getDayArrival(): ?\DateTimeInterface { return $this->day_arrival; } public function setDayArrival(\DateTimeInterface $day_arrival): self { $this->day_arrival = $day_arrival; return $this; } public function getArrivalTime(): ?\DateTimeInterface { return $this->arrival_time; } public function setArrivalTime(\DateTimeInterface $arrival_time): self { $this->arrival_time = $arrival_time; return $this; } public function getPrice(): ?int { return $this->price; } public function setPrice(int $price): self { $this->price = $price; return $this; } public function isDiscount(): ?bool { return $this->discount; } public function setDiscount(bool $discount): self { $this->discount = $discount; return $this; } public function getNumberPlace(): ?int { return $this->number_place; } public function setNumberPlace(int $number_place): self { $this->number_place = $number_place; return $this; } public function getDepartureCity(): ?DepartureCity { return $this->departureCity; } public function setDepartureCity(?DepartureCity $departureCity): self { $this->departureCity = $departureCity; return $this; } public function getArrivalCity(): ?ArrivalCity { return $this->arrivalCity; } public function setArrivalCity(?ArrivalCity $arrivalCity): self { $this->arrivalCity = $arrivalCity; return $this; } }
Merci d'avance aux personnes qui pourront m'aider.
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 <?php namespace App\Form; use App\Entity\Flights; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\Extension\Core\Type\TimeType; use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; class FlightsType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { $builder ->add('flight_number', TextType::class,[ 'label' => 'Flight number', 'attr' => [ 'placeholder' => "NH000" ]]) ->add('day', DateType::class,[ 'label' => 'Day of departure' ]) ->add('departure_time', TimeType::class,[ 'label' => 'Departure time', 'attr' => [ 'placeholder' => "08:30" ]]) ->add('day_arrival', DateType::class,[ 'label' => 'Day of arrival' ]) ->add('arrival_time', TimeType::class,[ 'label' => 'Arrival time', 'attr' => [ 'placeholder' => "17:30" ]]) ->add('price', IntegerType::class,[ 'label' => 'Price in ' ]) ->add('discount', CheckboxType::class,[ 'label' => 'Discount' ]) ->add('number_place', IntegerType::class,[ 'label' => 'Number of places' ]) ->add('departureCity') ->add('arrivalCity') ->add('submit', SubmitType::class); ; } public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'data_class' => Flights::class, ]); } }
Bien cordialement
Partager