IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

Symfony 2.5 - Formulaire [2.x]


Sujet :

Symfony PHP

  1. #1
    Nouveau membre du Club
    Inscrit en
    Janvier 2009
    Messages
    32
    Détails du profil
    Informations personnelles :
    Âge : 49

    Informations forums :
    Inscription : Janvier 2009
    Messages : 32
    Points : 33
    Points
    33
    Par défaut Symfony 2.5 - Formulaire
    Salut à tous,

    J'ai une difficulté en utilisant le crud généré par sf2
    Sur "indexAction" ou "showAction" mon objet est entièrement affiché.
    Lorsque je fais "editAction", l'attribut 'idadresse' n'est plus affiché et je dois le rentrer de nouveau afin qu'il ne s'efface pas de la base de données.

    J'aimerais bien qu'il me soit affiché et que je n'ai plus à le sélectionner de nouveau
    Je me casse la tête depuis 3 jours et ne trouve rien comme solution...Please une âme charitable pour me sortir de là?
    Merci d'avance.

    Mon entité:
    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
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    <?php
     
    namespace dk\SchoolManagerBundle\Entity;
     
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * Personne
     *
     * @ORM\Table(name="personne", indexes={@ORM\Index(name="FK_RESIDER", columns={"IDADRESSE"})})
     * @ORM\Entity
     */
    class Personne
    {
        /**
         * @var integer
         *
         * @ORM\Column(name="IDPERSONNE", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;
     
        /**
         * @var string
         *
         * @ORM\Column(name="NOMPERSONNE", type="string", length=55, nullable=false)
         */
        private $nompersonne;
     
        /**
         * @var string
         *
         * @ORM\Column(name="PRENOMPERSONNE", type="string", length=55, nullable=true)
         */
        private $prenompersonne;
     
        /**
         * @var string
         *
         * @ORM\Column(name="TYPEPERSONNE", type="string", length=20, nullable=true)
         */
        private $typepersonne;
     
        /**
         * @var string
         *
         * @ORM\Column(name="SEXEPERSONNE", type="string", length=1, nullable=true)
         */
        private $sexepersonne;
     
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="DATENAISSANCE", type="date", nullable=true)
         */
        private $datenaissance;
     
        /**
         * @var string
         *
         * @ORM\Column(name="MAILPERSONNE", type="string", length=55, nullable=true)
         */
        private $mailpersonne;
     
        /**
         * @var string
         *
         * @ORM\Column(name="TELPORTABLE", type="string", length=10, nullable=true)
         */
        private $telportable;
     
        /**
         * @var string
         *
         * @ORM\Column(name="COMMENTAIREPERSONNE", type="text", nullable=true)
         */
        private $commentairepersonne;
     
        /**
         * @var \Adresse
         *
         * @ORM\ManyToOne(targetEntity="Adresse")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="IDADRESSE", referencedColumnName="IDADRESSE")
         * })
         */
        private $idadresse;
     
     
     
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Set nompersonne
         *
         * @param string $nompersonne
         * @return Personne
         */
        public function setNompersonne($nompersonne)
        {
            $this->nompersonne = $nompersonne;
     
            return $this;
        }
     
        /**
         * Get nompersonne
         *
         * @return string 
         */
        public function getNompersonne()
        {
            return $this->nompersonne;
        }
     
        /**
         * Set prenompersonne
         *
         * @param string $prenompersonne
         * @return Personne
         */
        public function setPrenompersonne($prenompersonne)
        {
            $this->prenompersonne = $prenompersonne;
     
            return $this;
        }
     
        /**
         * Get prenompersonne
         *
         * @return string 
         */
        public function getPrenompersonne()
        {
            return $this->prenompersonne;
        }
     
        /**
         * Set typepersonne
         *
         * @param string $typepersonne
         * @return Personne
         */
        public function setTypepersonne($typepersonne)
        {
            $this->typepersonne = $typepersonne;
     
            return $this;
        }
     
        /**
         * Get typepersonne
         *
         * @return string 
         */
        public function getTypepersonne()
        {
            return $this->typepersonne;
        }
     
        /**
         * Set sexepersonne
         *
         * @param string $sexepersonne
         * @return Personne
         */
        public function setSexepersonne($sexepersonne)
        {
            $this->sexepersonne = $sexepersonne;
     
            return $this;
        }
     
        /**
         * Get sexepersonne
         *
         * @return string 
         */
        public function getSexepersonne()
        {
            return $this->sexepersonne;
        }
     
        /**
         * Set datenaissance
         *
         * @param \DateTime $datenaissance
         * @return Personne
         */
        public function setDatenaissance($datenaissance)
        {
            $this->datenaissance = $datenaissance;
     
            return $this;
        }
     
        /**
         * Get datenaissance
         *
         * @return \DateTime 
         */
        public function getDatenaissance()
        {
            return $this->datenaissance;
        }
     
        /**
         * Set mailpersonne
         *
         * @param string $mailpersonne
         * @return Personne
         */
        public function setMailpersonne($mailpersonne)
        {
            $this->mailpersonne = $mailpersonne;
     
            return $this;
        }
     
        /**
         * Get mailpersonne
         *
         * @return string 
         */
        public function getMailpersonne()
        {
            return $this->mailpersonne;
        }
     
        /**
         * Set telportable
         *
         * @param string $telportable
         * @return Personne
         */
        public function setTelportable($telportable)
        {
            $this->telportable = $telportable;
     
            return $this;
        }
     
        /**
         * Get telportable
         *
         * @return string 
         */
        public function getTelportable()
        {
            return $this->telportable;
        }
     
        /**
         * Set commentairepersonne
         *
         * @param string $commentairepersonne
         * @return Personne
         */
        public function setCommentairepersonne($commentairepersonne)
        {
            $this->commentairepersonne = $commentairepersonne;
     
            return $this;
        }
     
        /**
         * Get commentairepersonne
         *
         * @return string 
         */
        public function getCommentairepersonne()
        {
            return $this->commentairepersonne;
        }
     
        /**
         * Set idadresse
         *
         * @param \dk\SchoolManagerBundle\Entity\Adresse $idadresse
         * @return Personne
         */
        public function setId(\dk\SchoolManagerBundle\Entity\Adresse $idadresse = null)
        {
            $this->idadresse = $idadresse;
     
            return $this;
        }
     
        /**
         * Get idadresse
         *
         * @return \dk\SchoolManagerBundle\Entity\Adresse 
         */
        public function getIdadresse()
        {
            return $this->idadresse;
        }
     
        public function __toString()
        {
            return $this->nompersonne.' '.$this->prenompersonne;
        }
    }
    Mon 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
    <?php
     
    namespace dk\SchoolManagerBundle\Form;
     
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
     
    class PersonneType extends AbstractType
    {
            /**
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('nompersonne')
                ->add('prenompersonne')
                ->add('typepersonne', 'choice', array(
                            'choices' => array( 'Eleve'   => 'Eleve',
                                                'Enseignant' => 'Enseignant',
                                                'Parent' => 'Parent',
                                                'Prospect' => 'Prospect'),
                            'empty_value' => 'Choisir',
                            'required' => false,
                    ))
                ->add('sexepersonne','choice', array(
                            'choices'  => array('F'   => 'Féminin',
                                                'M' => 'Masculin',),
                            'empty_value'=>'Choisir',
                            'required' => false,
                    ))
                ->add('datenaissance', 'date', array(
                    'empty_value' => array('day' => 'Jour', 'month' => 'Mois', 'year' => 'Année'),
                    'years' => range(date('Y') + 0, date('Y') - 100),
                    'required' => false, 
                ))
                ->add('mailpersonne')
                ->add('telportable')
                ->add('id', 'entity', array (
                    'class'=>'dkSchoolManagerBundle:Adresse',
                    'label'=>'Adresse', 'empty_value'=>'Choisir',
                    'required' => false,)                 )
                ->add('commentairepersonne')
            ;
        }
     
        /**
         * @param OptionsResolverInterface $resolver
         */
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'dk\SchoolManagerBundle\Entity\Personne'
            ));
        }
     
        /**
         * @return string
         */
        public function getName()
        {
            return 'dk_schoolmanagerbundle_personne';
        }
    }
    Mon Controleur:
    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
    <?php
     
    namespace dk\SchoolManagerBundle\Controller;
     
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
    use dk\SchoolManagerBundle\Entity\Personne;
    use dk\SchoolManagerBundle\Form\PersonneType;
     
    /**
     * Personne controller.
     *
     * @Route("/personne")
     */
    class PersonneController extends Controller
    {
     
        /**
         * Lists all Personne entities.
         *
         * @Route("/", name="personne")
         * @Method("GET")
         * @Template()
         */
        public function indexAction()
        {
            $em = $this->getDoctrine()->getManager();
     
            $entities = $em->getRepository('dkSchoolManagerBundle:Personne')->findAll();
     
            return array(
                'entities' => $entities,
            );
        }
        /**
         * Creates a new Personne entity.
         *
         * @Route("/", name="personne_create")
         * @Method("POST")
         * @Template("dkSchoolManagerBundle:Personne:new.html.twig")
         */
        public function createAction(Request $request)
        {
            $entity = new Personne();
            $form = $this->createCreateForm($entity);
            $form->handleRequest($request);
     
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($entity);
                $em->flush();
     
                return $this->redirect($this->generateUrl('personne_show', array('id' => $entity->getId())));
            }
     
            return array(
                'entity' => $entity,
                'form'   => $form->createView(),
            );
        }
     
        /**
        * Creates a form to create a Personne entity.
        *
        * @param Personne $entity The entity
        *
        * @return \Symfony\Component\Form\Form The form
        */
        private function createCreateForm(Personne $entity)
        {
            $form = $this->createForm(new PersonneType(), $entity, array(
                'action' => $this->generateUrl('personne_create'),
                'method' => 'POST',
            ));
     
            $form->add('submit', 'submit', array('label' => 'Create'));
     
            return $form;
        }
     
        /**
         * Displays a form to create a new Personne entity.
         *
         * @Route("/new", name="personne_new")
         * @Method("GET")
         * @Template()
         */
        public function newAction()
        {
            $entity = new Personne();
            $form   = $this->createCreateForm($entity);
     
            return array(
                'entity' => $entity,
                'form'   => $form->createView(),
            );
        }
     
        /**
         * Finds and displays a Personne entity.
         *
         * @Route("/{id}", name="personne_show")
         * @Method("GET")
         * @Template()
         */
        public function showAction($id)
        {
            $em = $this->getDoctrine()->getManager();
     
            $entity = $em->getRepository('dkSchoolManagerBundle:Personne')->find($id);
     
            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Personne entity.');
            }
     
            $deleteForm = $this->createDeleteForm($id);
     
            return array(
                'entity'      => $entity,
                'delete_form' => $deleteForm->createView(),
            );
        }
     
        /**
         * Displays a form to edit an existing Personne entity.
         *
         * @Route("/{id}/edit", name="personne_edit")
         * @Method("GET")
         * @Template()
         */
        public function editAction($id)
        {
            $em = $this->getDoctrine()->getManager();
     
            $entity = $em->getRepository('dkSchoolManagerBundle:Personne')->find($id);
     
            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Personne entity.');
            }
     
            $editForm = $this->createEditForm($entity);
            $deleteForm = $this->createDeleteForm($id);
     
            return array(
                'entity'      => $entity,
                'edit_form'   => $editForm->createView(),
                'delete_form' => $deleteForm->createView(),
            );
        }
     
        /**
        * Creates a form to edit a Personne entity.
        *
        * @param Personne $entity The entity
        *
        * @return \Symfony\Component\Form\Form The form
        */
        private function createEditForm(Personne $entity)
        {
            $form = $this->createForm(new PersonneType(), $entity, array(
                'action' => $this->generateUrl('personne_update', array('id' => $entity->getId())),
                'method' => 'PUT',
            ));
     
            $form->add('submit', 'submit', array('label' => 'Update'));
     
            return $form;
        }
        /**
         * Edits an existing Personne entity.
         *
         * @Route("/{id}", name="personne_update")
         * @Method("PUT")
         * @Template("dkSchoolManagerBundle:Personne:edit.html.twig")
         */
        public function updateAction(Request $request, $id)
        {
            $em = $this->getDoctrine()->getManager();
     
            $entity = $em->getRepository('dkSchoolManagerBundle:Personne')->find($id);
     
            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Personne entity.');
            }
     
            $deleteForm = $this->createDeleteForm($id);
            $editForm = $this->createEditForm($entity);
            $editForm->handleRequest($request);
     
            if ($editForm->isValid()) {
                $em->flush();
     
                return $this->redirect($this->generateUrl('personne_show', array('id' => $id)));
            }
     
            return array(
                'entity'      => $entity,
                'edit_form'   => $editForm->createView(),
                'delete_form' => $deleteForm->createView(),
            );
        }
        /**
         * Deletes a Personne entity.
         *
         * @Route("/{id}", name="personne_delete")
         * @Method("DELETE")
         */
        public function deleteAction(Request $request, $id)
        {
            $form = $this->createDeleteForm($id);
            $form->handleRequest($request);
     
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $entity = $em->getRepository('dkSchoolManagerBundle:Personne')->find($id);
     
                if (!$entity) {
                    throw $this->createNotFoundException('Unable to find Personne entity.');
                }
     
                $em->remove($entity);
                $em->flush();
            }
     
            return $this->redirect($this->generateUrl('personne'));
        }
     
        /**
         * Creates a form to delete a Personne entity by id.
         *
         * @param mixed $id The entity id
         *
         * @return \Symfony\Component\Form\Form The form
         */
        private function createDeleteForm($id)
        {
            return $this->createFormBuilder()
                ->setAction($this->generateUrl('personne_delete', array('id' => $id)))
                ->setMethod('DELETE')
                ->add('submit', 'submit', array('label' => 'Delete'))
                ->getForm()
            ;
        }
    }

  2. #2
    Membre du Club
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2013
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2013
    Messages : 33
    Points : 68
    Points
    68
    Par défaut
    Bonjour,

    ce ne serait pas plutôt :

    à la ligne 41 ?

    De plus, au niveau de la conception de tes entités, je te conseille de bien penser "objet". Par exemple, je vois qu'ici une personne a un "idadresse". Or, tu utilises Doctrine2, donc il faut penser objet : une personne n'a pas une idadresse, mais bien une adresse . Il est plus logique de voir dans un code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    $personne->getAdresse()->getId();
    //Plutôt que :
    $personne->getIdAdresse()->getId();
    pour par exemple récupérer l'id de l'adresse de la personne.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Mai 2014
    Messages
    46
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux

    Informations forums :
    Inscription : Mai 2014
    Messages : 46
    Points : 34
    Points
    34
    Par défaut
    Tu es un génie génial

    Merci pour le conseil, je m'y mettrais pour optimiser le code

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [2.x] Symfony 2/doctrine2 formulaire de recherche par choix multiple
    Par laurentche dans le forum Symfony
    Réponses: 2
    Dernier message: 03/02/2014, 17h54
  2. [2.x] Symfony 2.3 - Formulaire - Problème avec form_widget
    Par legovitin dans le forum Symfony
    Réponses: 4
    Dernier message: 16/12/2013, 16h12
  3. Réponses: 2
    Dernier message: 04/07/2011, 17h22
  4. [1.x] symfony : inclure deux formulaires dans la même page
    Par kari.mourad dans le forum Débuter
    Réponses: 1
    Dernier message: 27/03/2011, 04h33
  5. [1.x] Formulaire de recherche symfony (débutant)
    Par T1T1b dans le forum Symfony
    Réponses: 23
    Dernier message: 15/05/2008, 13h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo