Bonjour!
Merci d'avance à ceux et celles qui prendront le temps de m'aider!

Pour les cours je dois créer un site web en partant d'une base de données existante. Les entités ont été généré via celle-ci, et une première version des form aussi.

J'ai notamment 2 entity qui sont reliées dans la base de donnée:
"Users" OneToMany "Appeler" ManyToOne "Contact"

voici les 3 entity:
Users

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
 
namespace DCB\ApprentissagesBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
 
/**
 * Users
 *
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="DCB\ApprentissagesBundle\Entity\UsersRepository")
 */
class Users {
 
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
 
    public function setId($id) {
        $this->id = $id;
    }
 
    public function getId() {
        return $this->id;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, nullable=false)
     */
    private $name;
 
    public function setName($name) {
        $this->name = $name;
    }
 
    public function getName() {
        return $this->name;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="role", type="string", length=25, nullable=false)
     */
    private $role;
 
    public function setRole($role) {
        $this->role = $role;
    }
 
    public function getRole() {
        return $this->role;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=false)
     */
    private $email;
 
    public function setEmail($email) {
        $this->email = $email;
    }
 
    public function getEmail() {
        return $this->email;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="password", type="string", length=60, nullable=true)
     */
    private $password;
 
    public function setPassword($password) {
        $this->password = $password;
    }
 
    public function getPassword() {
        return $this->password;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="remember_token", type="string", length=100, nullable=true)
     */
    private $rememberToken;
 
    public function setRememberToken($rememberToken) {
        $this->rememberToken = $rememberToken;
    }
 
    public function getRememberToken() {
        return $this->rememberToken;
    }
 
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
     */
    private $createdAt;
 
    public function setCreatedAt($createdAt) {
        $this->createdAt = $createdAt;
    }
 
    public function getCreatedAt() {
        return $this->createdAt;
    }
 
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
     */
    private $updatedAt;
 
    public function setUpdatedAt($updatedAt) {
        $this->updatedAt = $updatedAt;
    }
 
    public function getUpdatedAt() {
        return $this->updatedAt;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=50, nullable=true)
     */
    private $nom;
 
    public function setNom($nom) {
        $this->nom = $nom;
    }
 
    public function getNom() {
        return $this->nom;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="prenom", type="string", length=50, nullable=true)
     */
    private $prenom;
 
    public function setPrenom($nom) {
        $this->prenom = $nom;
    }
 
    public function getPrenom() {
        return $this->prenom;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="adresse", type="string", length=100, nullable=false)
     */
    private $adresse;
 
    public function setAdresse($adresse) {
        $this->adresse = $adresse;
    }
 
    public function getAdresse() {
        return $this->adresse;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="cp", type="string", length=100, nullable=false)
     */
    private $cp;
 
    public function setCp($cp) {
        $this->cp = $cp;
    }
 
    public function getCp() {
        return $this->cp;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="ville", type="string", length=100, nullable=false)
     */
    private $ville;
 
    public function setVille($ville) {
        $this->ville = $ville;
    }
 
    public function getVille() {
        return $this->ville;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="telephone", type="string", length=20, nullable=false)
     */
    private $telephone;
 
    public function setTelephone($telephone) {
        $this->telephone = $telephone;
    }
 
    public function getTelephone() {
        return $this->telephone;
    }
 
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="dob", type="date", nullable=false)
     */
    private $dob;
 
    public function setDob($dob) {
        $this->dob = $dob;
    }
 
    public function getDob() {
        return $this->dob;
    }
 
    /**
     * @ORM\OneToMany(targetEntity="DCB\ApprentissagesBundle\Entity\Contact",mappedBy="contact", cascade={"persist"})
     */
    private $contacts;
 
    public function addContacts($contact) {
        $this->contacts[] = $contact;
    }
 
    public function getContact() {
        return $this->contacts;
    }
 
    public function removeContact($contact) {
        $this->contacts->removeElement($contact);
    }
 
    public function __construct(){
        $this->contacts = new ArrayCollection();
    }
}


Appeler

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
<?php
 
namespace DCB\ApprentissagesBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
 
/**
 * Appeller
 *
 * @ORM\Table(name="appeller", indexes={@ORM\Index(name="FK_appeller_id_contact", columns={"id_contact"}), @ORM\Index(name="fk_appeller_users1_idx", columns={"users_id"})})
 * @ORM\Entity(repositoryClass="DCB\ApprentissagesBundle\Entity\AppellerRepository")
 */
class Appeller {
 
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
 
    public function setId($id) {
        $this->id = $id;
    }
 
    public function getId() {
        return $this->id;
    }
 
    /**
     * @var \Contact
     *
     * @ORM\ManyToOne(targetEntity="Contact")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_contact", referencedColumnName="id_contact")
     * })
     */
    private $contact;
 
    public function setContact($contact) {
        $this->contact = $contact;
    }
 
    public function getContact() {
        return $this->contact;
    }
 
    /**
     * @var \Users
     *
     * @ORM\ManyToOne(targetEntity="Users")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="users_id", referencedColumnName="id")
     * })
     */
    private $users;
 
    public function setUsers($users) {
        $this->users = $users;
    }
 
    public function getUsers() {
        return $this->users;
    }
}


Contact

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
<?php
 
namespace DCB\ApprentissagesBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
 
/**
 * Contact
 *
 * @ORM\Table(name="contact")
 * @ORM\Entity
 */
class Contact {
 
    /**
     * @var integer
     *
     * @ORM\Column(name="id_contact", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $idContact;
 
    public function setIdContact($idContact) {
        $this->idContact = $idContact;
    }
 
    public function getIdContact() {
        return $this->idContact;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="nom_contact", type="string", length=25, nullable=true)
     */
    private $nomContact;
 
    public function setNomContact($nomContact) {
        $this->nomContact = $nomContact;
    }
 
    public function getNomContact() {
        return $this->nomContact;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="prenom_contact", type="string", length=25, nullable=true)
     */
    private $prenomContact;
 
    public function setPrenomContact($prenomContact) {
        $this->prenomContact = $prenomContact;
    }
 
    public function getPrenomContact() {
        return $this->prenomContact;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="adrese_contact", type="string", length=100, nullable=true)
     */
    private $adreseContact;
 
    public function setAdreseContact($adreseContact) {
        $this->adreseContact = $adreseContact;
    }
 
    public function getAdreseContact() {
        return $this->adreseContact;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="CP_contact", type="string", length=6, nullable=true)
     */
    private $cpContact;
 
    public function setCpContact($cpContact) {
        $this->cpContact = $cpContact;
    }
 
    public function getCpContact() {
        return $this->cpContact;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="ville_contact", type="string", length=25, nullable=true)
     */
    private $villeContact;
 
    public function setVilleContact($villeContact) {
        $this->villeContact = $villeContact;
    }
 
    public function getVilleContact() {
        return $this->villeContact;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="telFixe_contact", type="string", length=10, nullable=true)
     */
    private $telfixeContact;
 
    public function setTelfixeContact($telfixeContact) {
        $this->telfixeContact = $telfixeContact;
    }
 
    public function getTelfixeContact() {
        return $this->telfixeContact;
    }
 
    /**
     * @var string
     *
     * @ORM\Column(name="telMobile_contact", type="string", length=10, nullable=true)
     */
    private $telmobileContact;
 
    public function setTelmobileContact($telmobileContact) {
        $this->telmobileContact = $telmobileContact;
    }
 
    public function getTelmobileContact() {
        return $this->telmobileContact;
    }
 
}


Voici les Form:
UsersType

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
<?php
 
namespace DCB\ApprentissagesBundle\Form;
 
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\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\CallbackTransformer;
 
class UsersType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', TextType::class, array('label' => 'Nom:'))
                ->add('prenom', TextType::class, array('label' => 'Prénom:'))
                ->add('role', TextType::class)
                ->add('email', EmailType::class, array('label' => 'Email:'))
                ->add('password', PasswordType::class)
                ->add('createdAt', DateType::class)
                ->add('updatedAt', DateType::class)
                ->add('adresse', TextType::class, array('label' => 'Adresse:'))
                ->add('cp', TextType::class, array('label' => 'Code Postal:'))
                ->add('ville', TextType::class, array('label' => 'Ville:'))
                ->add('telephone', TextType::class, array('label' => 'Téléphone:'))
                ->add('dob', BirthdayType::class, array('label' => 'Date de naissance:'))
                //->add('contact', ContactType::class, array('data_class' => null))
                ->add('contact', CollectionType::class, array(
                                                    'entry_type' => ContactType::class,
                                                    'allow_add' => TRUE,
                                                    'allow_delete' => TRUE
                                               )
                      )
                ->add('Enregistrer', SubmitType::class);
 
        /*$builder->get('contact')
            ->addModelTransformer(new CallbackTransformer(
                function ($contactAsArray) {
                    // transform the array to a string
                    return implode(', ', $contactAsArray);
                },
                function ($contactAsString) {
                    // transform the string back to an array
                    return explode(', ', $contactAsString);
                }
            ))
        ;*/
    }
 
    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'DCB\ApprentissagesBundle\Entity\Users'
        ));
    }
 
    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'dcb_apprentissagesbundle_users';
    }
 
 
}


ContactType

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
<?php
 
namespace DCB\ApprentissagesBundle\Form;
 
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\SubmitType;
 
class ContactType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('nomContact', TextType::class)
                ->add('prenomContact', TextType::class)
                ->add('adreseContact', TextType::class)
                ->add('cpContact', TextType::class)
                ->add('villeContact', TextType::class)
                ->add('telfixeContact', TextType::class)
                ->add('telmobileContact', TextType::class)
                ->add('Enregistrer', SubmitType::class);
    }
 
    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'DCB\ApprentissagesBundle\Entity\Contact'
        ));
    }
 
    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'dcb_apprentissagesbundle_contact';
    }
 
 
}


Vu twig:

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
{% extends "DCBApprentissagesBundle::base.html.twig"%}
{% block corps %}
    <h3>Ajout d'un élève</h3>
    {{dump(form)}}
    {{ form_start(form, {'attr': {'class': 'formEleve'}}) }}
        {{ form_errors(form) }}
 
        {#Identite eleve#}
        {{ form_row(form.name) }}
        {{ form_row(form.prenom) }}
        {{ form_row(form.dob) }}
 
        {#Information personnelle#}
        {{ form_row(form.email) }}
        {{ form_row(form.telephone) }}
 
        {#Habitation#}
        {{ form_row(form.adresse) }}
        {{ form_row(form.cp) }}
        {{ form_row(form.ville) }}
 
        {{ form_rest(form) }}
 
    <h3>Ajout plusieurs élèves</h3>
{% endblock %}



Mon problème est dans UsersType.
Je veux pouvoir saisir saisir plusieurs contacts lors de la saisie d'un user. J'ai cherché sur internet et trouvé plusieurs solutions, toute me levaient une erreur.. Sauf celle que vous pouvez voir:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
->add('contact', CollectionType::class, array(
                                                    'entry_type' => ContactType::class,
                                                    'allow_add' => TRUE,
                                                    'allow_delete' => TRUE
                                               )
                      )
Ca n'affiche rien. Enfin le UsersType s'affiche bien mais pas ContactType.

Quand je met cette ligne à la place le ContactType s'affiche bien.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
->add('contact', ContactType::class, array('data_class' => null))
J'ai fait un dump du form pour voir ce qu'il contenait avec mon CollectionType et son enfant contact est un FormView vide sans enfant:
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
FormView {#295 ▼
  +vars: array:24 []
  +parent: null
  +children: array:15 ["name" => FormView {#730 ▶}
    "prenom" => FormView {#421 ▶}
    "role" => FormView {#403 ▶}
    "email" => FormView {#382 ▶}
    "password" => FormView {#664 ▶}
    "createdAt" => FormView {#370 ▶}
    "updatedAt" => FormView {#618 ▶}
    "adresse" => FormView {#837 ▶}
    "cp" => FormView {#848 ▶}
    "ville" => FormView {#851 ▶}
    "telephone" => FormView {#853 ▶}
    "dob" => FormView {#855 ▶}
    "contact" => FormView {#857 ▼
      +vars: array:27 []
      +parent: FormView {#295}
      +children: []
      -rendered: false
    }
    "Enregistrer" => FormView {#986 ▶}
    "_token" => FormView {#1008 ▶}
  ]
  -rendered: false
}
Voici comment je créer le formulaire dans UsersController:
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
public function addAction(Request $request) {
        //$request = $this->get('request');
        $eleve = new Users();
 
        $form = $this->createForm(UsersType::class, $eleve);
        $form->handleRequest($request);
 
        if($form->isSubmitted() && $form->isValid()){
            //définition des valeurs non modifables par l'utilisateur
            $eleve->setRole('eleve');
            $eleve->setCreatedAt(new \DateTime());
            $eleve->setUpdatedAt(new \DateTime());
 
            //insertion en BdD
            $em = $this->getDoctrine()->getManager();
            $em->persist($eleve);
            $em->flush();
 
            //Redirection sur la page de vue d'un eleve
            return $this->redirect(
                    $this->generateUrl('dcb_apprentissages_view_eleve',
                        array('id' => $eleve->getId())
                    )
            );
        }
 
        //Page d'affichage du formulaire
        return $this->render('DCBApprentissagesBundle:Eleve:addEleve.html.twig',
                array('form' => $form->createView())
        );
    }


Quelqu'un sait-il pourquoi??
Encore merci!!

Je voulais aussi savoir, pour l'affichage des utilisateurs avec leurs contacts et leur classe (encore une autre table), est-ce que je peux gérer ça avec des formulaires ou je suis obligé d'y faire avec le queryBuilder??

P.S:
Débutant sur symfony! (1er projet)