Bonjour, j'ai un problème lorsque je veux mettre en oeuvre mon héritage sous Symfony.
J'ai 2 sous-classes : Location et Vente qui héritent de la super-classe Bien.
Avec doctrine j'ai réalisé ceci :
CLASSE BIEN
CLASSE VENTE
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 <?php namespace CL\LesGenetsBundle\Entity; use Doctrine\ORM\Mapping as ORM; use CL\LesGenetsBundle\Entity\Vente; use CL\LesGenetsBundle\Entity\Location; use Symfony\Component\Validator\Constraints as Assert; /** * CL\LesGenetsBundle\Entity\Bien * * @ORM\Table() * @ORM\Entity(repositoryClass="CL\LesGenetsBundle\Entity\BienRepository") * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorMap({"Vente" = "Vente", "Location" = "Location"}) */ class Bien { /** * @var integer $id * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") * @Assert\NotBlank() */ private $id; /** * @var string $photo * * @ORM\Column(name="photo", type="string", length=128, nullable=false) * @Assert\NotBlank() */ private $photo; /** * @var float $diagDPEA * * @ORM\Column(name="diagDPEA", type="float", length=2048, nullable=false) * @Assert\NotBlank() */ private $diagDPEA; /** * @var float $diagDPEB * * @ORM\Column(name="diagDPEB", type="float", length=2048, nullable=false) * @Assert\NotBlank() */ private $diagDPEB; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set photo * * @param string $photo */ public function setPhoto($photo) { $this->photo = $photo; } /** * Get photo * * @return string */ public function getPhoto() { return $this->photo; } /** * Set diagDPEA * * @param float $diagDPEA */ public function setDiagDPEA($diagDPEA) { $this->diagDPEA = $diagDPEA; } /** * Get diagDPEA * * @return float */ public function getDiagDPEA() { return $this->diagDPEA; } /** * Set diagDPEB * * @param float $diagDPEB */ public function setDiagDPEB($diagDPEB) { $this->diagDPEB = $diagDPEB; } /** * Get diagDPEB * * @return float */ public function getDiagDPEB() { return $this->diagDPEB; } }
CLASSE LOCATION
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 <?php namespace CL\LesGenetsBundle\Entity; use Doctrine\ORM\Mapping as ORM; use CL\LesGenetsBundle\Entity\Bien; use Symfony\Component\Validator\Constraints as Assert; /** * CL\LesGenetsBundle\Entity\Vente * * @ORM\Table() * @ORM\Entity(repositoryClass="CL\LesGenetsBundle\Entity\VenteRepository") */ class Vente extends Bien { /** * @var float $prixAppart * * @ORM\Column(name="prixAppart", type="float", length=2048, nullable=false) */ private $prixAppart; /** * @var float $prixCave * * @ORM\Column(name="prixCave", type="float", length=2048, nullable=false) */ private $prixCave; /** * @var float $prixGarage * * @ORM\Column(name="prixGarage", type="float", length=2048, nullable=false) */ private $prixGarage; /** * @var float $prixParking * * @ORM\Column(name="prixParking", type="float", length=2048, nullable=false) */ private $prixParking; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set prixAppart * * @param float $prixAppart */ public function setPrixAppart($prixAppart) { $this->prixAppart = $prixAppart; } /** * Get prixAppart * * @return float */ public function getPrixAppart() { return $this->prixAppart; } /** * Set prixCave * * @param float $prixCave */ public function setPrixCave($prixCave) { $this->prixCave = $prixCave; } /** * Get prixCave * * @return float */ public function getPrixCave() { return $this->prixCave; } /** * Set prixGarage * * @param float $prixGarage */ public function setPrixGarage($prixGarage) { $this->prixGarage = $prixGarage; } /** * Get prixGarage * * @return float */ public function getPrixGarage() { return $this->prixGarage; } /** * Set prixParking * * @param float $prixParking */ public function setPrixParking($prixParking) { $this->prixParking = $prixParking; } /** * Get prixParking * * @return float */ public function getPrixParking() { return $this->prixParking; } }
Maintenant, le problème : (Le formulaire, la vue et le controller existent)
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 <?php namespace CL\LesGenetsBundle\Entity; use Doctrine\ORM\Mapping as ORM; use CL\LesGenetsBundle\Entity\Bien; /** * CL\LesGenetsBundle\Entity\Location * * @ORM\Table() * @ORM\Entity(repositoryClass="CL\LesGenetsBundle\Entity\LocationRepository") */ class Location extends Bien { /** * @var float $loyerAppart * * @ORM\Column(name="loyerAppart", type="float", length=2048, nullable=false) */ private $loyerAppart; /** * @var float $loyerGarage * * @ORM\Column(name="loyerGarage", type="float", length=2048, nullable=false) */ private $loyerGarage; /** * @var float $loyerParking * * @ORM\Column(name="loyerParking", type="float", length=2048, nullable=false) */ private $loyerParking; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set loyerAppart * * @param float $loyerAppart */ public function setLoyerAppart($loyerAppart) { $this->loyerAppart = $loyerAppart; } /** * Get loyerAppart * * @return float */ public function getLoyerAppart() { return $this->loyerAppart; } /** * Set loyerGarage * * @param float $loyerGarage */ public function setLoyerGarage($loyerGarage) { $this->loyerGarage = $loyerGarage; } /** * Get loyerGarage * * @return float */ public function getLoyerGarage() { return $this->loyerGarage; } /** * Set loyerParking * * @param float $loyerParking */ public function setLoyerParking($loyerParking) { $this->loyerParking = $loyerParking; } /** * Get loyerParking * * @return float */ public function getLoyerParking() { return $this->loyerParking; } }
Je veux ajouter une vente et lors de la validation, j'ai l'erreur the CRSF token is invalid... Après des recherches sur internet j'ai pu constater que ce problème pouvait survenir lors que l'on essaye de submit plus d'un formulaire à la fois.
Or je submit Location qui extend Bien, est-ce que vous pensez que c'est à cause de ça que j'obtiens ce message d'erreur ?
FORM
VUE
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 <?php namespace CL\LesGenetsBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; use CL\LesGenetsBundle\Entity\Vente; class VenteType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder ->add('photo', 'text') ->add('diagDPEA', 'text') ->add('diagDPEB', 'text') ->add('prixAppart', 'text') ->add('prixCave', 'text') ->add('prixGarage', 'text') ->add('prixParking', 'text') ; } public function getName() { return 'CL_LesGenetsBundle_VenteType'; }
CONTROLLER
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 {{ form_errors(form) }} <form action="{{ path('ajouterVente') }}" method="post" {{ form_enctype(form) }}> {{ form_widget(form) }} {{ form_rest(form) }} </form>
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 public function ajouterVenteAction() { $vente = new Vente(); $request = $this->getRequest(); $form = $this->createForm(new VenteType(), $vente); if ($request->getMethod() == 'POST') { $form->bindRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getEntityManager(); $em->persist($vente); $em->flush(); $form = $this->createForm(new VenteType(), new Vente()); return $this->redirect($this->generateUrl('ajoutEffectue', array())); } } return $this->render( 'CLLesGenetsBundle:Vente:ajouterVente.html.twig', array( 'form' => $form->createView() ) ); }
Partager