Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8543129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:13:48+00:00 2026-06-11T12:13:48+00:00

I am using many to many relations in my symfony2 project, i have successfully

  • 0

I am using many to many relations in my symfony2 project, i have successfully made two entities ‘Users’ and Groups and i have also successfully persisted data which inserts data into users,groups and users_groups table by using

$user = new User();
$user->getGroups()->add($group);

now i want to edit the user which should also edit users_groups record..

I have searched a lot but no luck.Any help would be appreciated …

CONTROLLER

   public function editAction()
  {
  if($this->getRequest()->query->get("id")){
        $id = $this->getRequest()->query->get("id");
        $request = $this->getRequest();

        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('DesignAppBundle:Users')->find($id);


        $editForm = $this->createForm(new UserType());


        if ($this->getRequest()->getMethod() == 'POST') {
        $editForm->bindRequest($request);
        if ($editForm->isValid()) {

                           $postData = $request->request->get('users'); 


                            $repository = $this->getDoctrine()
                            ->getRepository('Bundle:groups');
                            $group = $repository->findOneById($postData['group']);

                            $entity->addGroups($group );

                            $em->flush();

                            return $this->redirect($this->generateUrl('index'));            
            }

        }

        return $this->render('User.html.twig',array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
        ));
    }       
  }

FORM

<?php

use and include .....


class UserType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
    $request = Request::createFromGlobals();

         $builder->add('group', 'entity', array(
         'class' => 'Bundle:Groups',
         'property' => 'name',
         'empty_value' => 'All',
         'required' => true,
             'multiple'=>true,
         'data' => $request->get('group_id')
        ));



    }

    public function getDefaultOptions(array $options)
    {

        return array(
            'validation_groups' => array('users'),
            'csrf_protection' => false,

        );
    }

    public function getName()
    {
        return 'users';
    }

}

User

 /**
     * @ORM\ManyToMany(targetEntity="Groups", inversedBy="users")
     * @ORM\JoinTable(name="users_groups",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     *      )
     **/  
    protected $group;

 public function __construct()
{
    $this->group= new ArrayCollection();        

}

  public function addgroups(\Design\AppBundle\Entity\Categories $group)
    {
        $this->group[] = $group;
    }

Group

/**
     * @ORM\ManyToMany(targetEntity="Users", mappedBy="group")
     */
    protected $users;

    public function __construct()
    {
        $this->users= new ArrayCollection();
    }

    /**
     * Add users
     *
     * @param Bundle\Entity\Users $users
     */
    public function addUsers(Bundle\Users $users)
    {
        $this->users[] = $users;
    }

    /**
     * Get users
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getUsers()
    {
        return $this->users;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:13:49+00:00Added an answer on June 11, 2026 at 12:13 pm

    You should not be adding the group_id, you should be adding the groups property

         $builder->add('groups');
    

    The group_id column is automatically handled by Doctrine and you should not be handling it yourself directly. Do you have a group_id property? If you do, you should remove it.

    The group entity should be:

    /**
     * @ORM\ManyToMany(targetEntity="Users", mappedBy="group")
     */
    protected $users;
    
    public function __construct()
    {
        $this->users= new ArrayCollection();
    }
    
    /**
     * Add users
     *
     * @param Bundle\Entity\User $user
     */
    public function addUsers(Bundle\User $users)
    {
        $this->users[] = $users;
    }
    
    /**
     * Get users
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getUsers()
    {
        return $this->users;
    }
    

    EDIT

    This is how your controller should be:

    public function editAction($request)
    {
        $id = $this->getRequest()->query->get("id");
    
        $em = $this->getDoctrine()->getEntityManager();
    
        $entity = $em->getRepository('DesignAppBundle:Users')->find($id);
    
        $editForm = $this->createForm(new UserType(),$entity);
    
        if ($this->getRequest()->getMethod() == 'POST') {
            $editForm->bindRequest($request);
            if ($editForm->isValid()) {
                 $em->persits($entity);
    
                 $em->flush();
    
                 return $this->redirect($this->generateUrl('index'));            
            }
    
        }
    
        return $this->render('User.html.twig',array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
        ));
    }
    

    and this is how your UserType should be:

    class UserType extends AbstractType
    {
        public function buildForm(FormBuilder $builder, array $options)
        {
    
             $builder->add('group', 'entity', array(
             'class' => 'Bundle:Groups',
             'property' => 'name',
             'empty_value' => 'All',
             'required' => true,
              'multiple'=>true,
            ));
    
        }
    
        public function getDefaultOptions(array $options)
        {
    
            return array(
                'validation_groups' => array('users'),
                'csrf_protection' => false,
    
            );
        }
    
        public function getName()
        {
            return 'users';
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a project that’s using many C++11 facilities, and we thought about this
I have a many-to-many relationship defined in my Symfony (using doctrine) project between Orders
I'm using Symfony and Doctrine, and have several many-to-many relations that work fine. But
I have two classes in Grails application , Employee has hasMany(one To Many relations)
I have 3 tables. users, campaigns, links. They have one-to-many relations. User have many
I have a simple application using ADO.NET EntityFramework using a many-to-many relationship between two
When using generateModelsFromDb to generate the models Doctrine makes one to many relations between
I'm using NHibernate to persist a many-to-many relation between Users and Networks. I've set
I have a variable that I'm using many times in a class and as
I want to share my session variables in PHP using many subdomains. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.