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 :

Relation ManyToMany et Collection


Sujet :

Symfony PHP

  1. #1
    Futur Membre du Club
    Homme Profil pro
    AFPA
    Inscrit en
    Novembre 2008
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : AFPA
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2008
    Messages : 20
    Points : 9
    Points
    9
    Par défaut Relation ManyToMany et Collection
    Bonjour, j'ai un soucis sous Symfony2

    J'ai 4 entités :

    Contact qui étent de FOSUserbundle
    Company
    ContactsCompanies
    Position

    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
    <?php
     
    namespace Site\UserBundle\Entity;
     
    use FOS\UserBundle\Entity\User as BaseUser;
    use Doctrine\ORM\Mapping as ORM;
     
    /**
     * @ORM\Entity
     * @ORM\Table(name="Contacts")
     */
    class Contact extends BaseUser
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
     
     
        /**
         * @ORM\OneToMany(targetEntity="Site\CoreBundle\Entity\ContactsCompanies", mappedBy="contact", cascade={"persist"})
         */
        protected $contacts_companies;
     
        /**
         * Get id
         *
         * @return integer
         */
        public function getId()
        {
            return $this->id;
        }
     
        /**
         * Constructor
         */
        public function __construct()
        {
            parent::__construct();
            $this->contacts_companies = new \Doctrine\Common\Collections\ArrayCollection();
        }
     
        /**
         * Add contacts_companies
         *
         * @param \Site\CoreBundle\Entity\ContactsCompanies $contactsCompanies
         * @return Contact
         */
        public function addContactsCompanie(\Site\CoreBundle\Entity\ContactsCompanies $contactsCompanies)
        {
            $this->contacts_companies[] = $contactsCompanies;
     
            return $this;
        }
     
        /**
         * Remove contacts_companies
         *
         * @param \Site\CoreBundle\Entity\ContactsCompanies $contactsCompanies
         */
        public function removeContactsCompanie(\Site\CoreBundle\Entity\ContactsCompanies $contactsCompanies)
        {
            $this->contacts_companies->removeElement($contactsCompanies);
        }
     
        /**
         * Get contacts_companies
         *
         * @return \Doctrine\Common\Collections\Collection
         */
        public function getContactsCompanies()
        {
            return $this->contacts_companies;
        }
     
     
     
    }
    Mon entité Company est similaire a mon entité Contact .

    Mon formType 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
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
     
     
        $builder
            ->add('username', null, array('label' => 'contact.form.username', 'attr' => array('placeholder' => 'contact.form.username', 'class' => 'col-sm-6')))
            ->add('email', null, array('label' => 'contact.form.email', 'attr' => array('placeholder' => 'contact.form.email', 'class' => 'col-sm-6')))
            ->add('password', 'password', array('label' => 'contact.form.password', 'attr' => array('placeholder' => 'contact.form.password', 'class' => 'col-sm-6')))
            ->add('firstname', null, array('label' => 'contact.form.firstname', 'attr' => array('placeholder' => 'contact.form.firstname', 'class' => 'col-sm-6')))
            ->add('lastname', null, array('label' => 'contact.form.lastname', 'attr' => array('placeholder' => 'contact.form.lastname', 'class' => 'col-sm-6')))
            ->add('phone', null, array('label' => 'contact.form.phone', 'attr' => array('placeholder' => 'contact.form.phone', 'class' => 'col-sm-6')))
            ->add('fax', null, array('label' => 'contact.form.fax', 'attr' => array('placeholder' => 'contact.form.fax', 'class' => 'col-sm-6')))
            ->add('mobilephone', null, array('label' => 'contact.form.mobilephone', 'attr' => array('placeholder' => 'contact.form.mobilephone', 'class' => 'col-sm-6')))
            ->add('job', null, array('label' => 'contact.form.job', 'attr' => array('placeholder' => 'contact.form.job', 'class' => 'col-sm-6')))
        ;
     
        $builder->add('contacts_companies', 'collection', array('type' => new ContactCompanyType(), 'label'=> false));
     
    }
    Mon ContactsCompaniesType:

    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
       /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('position', 'entity', array(
                    'label' => false,
                    'class'    => 'SiteCoreBundle:Position',
                    'property' => 'name',
                    'multiple' => false)
            )
     
        ;
     
    }
    Dans mon controlleur j'ai

    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
    /**
      * Creates a new User entity.
      *
      */
     public function createAction(Request $request)
     {
         $entity = new User();
         $form = $this->createCreateForm($entity);
         $form->handleRequest($request);
     
         if ($form->isValid())
         {
             $companyid = $this->get('site_user.provider')->getUser()->getDefaultcompany();
             $company = $this->getDoctrine()
                 ->getRepository('SiteCoreBundle:Company')
                 ->find($companyid);
     
             // On persist la companie et on l'insert en base
             $em = $this->getDoctrine()->getManager();
     
             $userManager = $this->container->get('fos_user.user_manager');
             $newUser = $userManager->createUser($entity);
             $newUser = $entity;
     
             $newUser->setPlainPassword($entity->getPassword());
             $newUser->setEnabled(true);
             $newUser->setDefaultcompany($companyid);
             $userManager->updateUser($newUser);
     
             // On créer un objet ContactsCompanies et on le persist
             $contact_company = new ContactsCompanies();
             $contact_company->setUser($newUser);
             $contact_company->setCompany($company);
             $contact_company->setPosition($entity->getPosition());
             $em->persist($contact_company);
     
             // On ajoute en base les objets persistés
             $em->flush();
     
             return $this->redirect($this->generateUrl('contact_show', array('id' => $entity->getId())));
         }
     
         return $this->render('SiteCoreBundle:Contact:new.html.twig', array(
             'entity' => $entity,
             'form'   => $form->createView(),
         ));
     }
    Le soucis en gros c'est que mon formulaire "Contact" s'affiche bien, sauf que j'ai pas l'attribut Position (donc mon select) qui dépend de ContactsCompanies

    Si quelqu'un peut m'aider ce serait génial.

    Dans le même temps vu que je débute sous SF si quelqu'un peu me donner des conseils sur ma synthaxe ce ne serait pas de refus

    Merci d'avance

  2. #2
    Futur Membre du Club
    Homme Profil pro
    AFPA
    Inscrit en
    Novembre 2008
    Messages
    20
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : AFPA
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2008
    Messages : 20
    Points : 9
    Points
    9
    Par défaut
    Une idée à mon soucis?
    Merci d'avance

Discussions similaires

  1. Réponses: 8
    Dernier message: 27/02/2009, 01h37
  2. aide sur relation manytomany
    Par Jacobian dans le forum JPA
    Réponses: 1
    Dernier message: 29/05/2008, 20h11
  3. EJB3 Relation ManyToMany
    Par cow_boy17 dans le forum JPA
    Réponses: 1
    Dernier message: 21/03/2008, 10h12
  4. [HQL] Pb avec relation ManyToMany
    Par jc63 dans le forum Hibernate
    Réponses: 1
    Dernier message: 26/07/2007, 14h35
  5. [NHibernate.Mapping.Attributes] Relation ManyToMany
    Par anthyme dans le forum NHibernate
    Réponses: 2
    Dernier message: 12/07/2007, 20h34

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