Symfony2实体集合 - 如何添加/删除与现有实体的关联?

69

1. 快速概览

1.1 目标

我想要创建/编辑用户工具。可编辑字段如下:

  • 用户名(类型:文本)
  • 密码(类型:密码)
  • 电子邮件(类型:电子邮件)
  • 组(类型:集合)
  • avoRoles(类型:集合)

注意:最后一个属性不是命名为$roles,因为我的User类扩展了FOSUserBundle的User类并覆盖了角色,这带来了更多问题。为了避免这些问题,我决定将我的角色集合存储在$avoRoles下面。

1.2 用户界面

我的模板包括两个部分:

  1. 用户表单
  2. 显示$userRepository->findAllRolesExceptOwnedByUser($user)的表格;

注意:findAllRolesExceptOwnedByUser()是自定义存储库函数,返回所有角色的子集(尚未分配给$user的角色)。

1.3 需求功能

1.3.1 添加角色:

     用户在角色表中单击 "+"(添加)按钮时  
    然后 jquery从角色表中删除该行  
    并且jquery向用户表单 (avoRoles 列表) 添加新的列表项

1.3.2 删除角色:

     用户在用户表单(avoRoles列表)中单击 "x"(删除)按钮时  
    然后 jquery从用户表单(avoRoles列表)中删除该列表项  
    并且 jquery向角色表中添加新行

1.3.3 保存更改:

     用户单击 "Zapisz"(保存)按钮时  
    然后 用户表单提交所有字段(用户名、密码、电子邮件、avoRoles、组)  
    并且 将avoRoles保存为Role实体的ArrayCollection(ManyToMany关系)  
    并且将groups保存为Role实体的ArrayCollection(ManyToMany关系)

注意:仅可以分配现有的Roles和Groups给User。如果由于任何原因找不到它们,则表单不应验证。


2. 代码

在此部分中,我呈现/简要描述了这个操作背后的代码。如果描述不足以及需要查看代码,请告诉我,我会粘贴它。我不会一开始就将所有代码粘贴过来,以避免向您发送不必要的代码。

2.1 用户类

我的用户类扩展了FOSUserBundle用户类。

namespace Avocode\UserBundle\Entity;

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Avocode\CommonBundle\Collections\ArrayCollection;
use Symfony\Component\Validator\ExecutionContext;

/**
 * @ORM\Entity(repositoryClass="Avocode\UserBundle\Repository\UserRepository")
 * @ORM\Table(name="avo_user")
 */
class User extends BaseUser
{
    const ROLE_DEFAULT = 'ROLE_USER';
    const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\generatedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToMany(targetEntity="Group")
     * @ORM\JoinTable(name="avo_user_avo_group",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
     * )
     */
    protected $groups;

    /**
     * @ORM\ManyToMany(targetEntity="Role")
     * @ORM\JoinTable(name="avo_user_avo_role",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
     * )
     */
    protected $avoRoles;

    /**
     * @ORM\Column(type="datetime", name="created_at")
     */
    protected $createdAt;

    /**
     * User class constructor
     */
    public function __construct()
    {
        parent::__construct();

        $this->groups = new ArrayCollection();        
        $this->avoRoles = new ArrayCollection();
        $this->createdAt = new \DateTime();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set user roles
     * 
     * @return User
     */
    public function setAvoRoles($avoRoles)
    {
        $this->getAvoRoles()->clear();

        foreach($avoRoles as $role) {
            $this->addAvoRole($role);
        }

        return $this;
    }

    /**
     * Add avoRole
     *
     * @param Role $avoRole
     * @return User
     */
    public function addAvoRole(Role $avoRole)
    {
        if(!$this->getAvoRoles()->contains($avoRole)) {
          $this->getAvoRoles()->add($avoRole);
        }

        return $this;
    }

    /**
     * Get avoRoles
     *
     * @return ArrayCollection
     */
    public function getAvoRoles()
    {
        return $this->avoRoles;
    }

    /**
     * Set user groups
     * 
     * @return User
     */
    public function setGroups($groups)
    {
        $this->getGroups()->clear();

        foreach($groups as $group) {
            $this->addGroup($group);
        }

        return $this;
    }

    /**
     * Get groups granted to the user.
     *
     * @return Collection
     */
    public function getGroups()
    {
        return $this->groups ?: $this->groups = new ArrayCollection();
    }

    /**
     * Get user creation date
     *
     * @return DateTime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }
}

2.2 角色类

我的角色类是扩展Symfony安全组件核心角色类的。

namespace Avocode\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Avocode\CommonBundle\Collections\ArrayCollection;
use Symfony\Component\Security\Core\Role\Role as BaseRole;

/**
 * @ORM\Entity(repositoryClass="Avocode\UserBundle\Repository\RoleRepository")
 * @ORM\Table(name="avo_role")
 */
class Role extends BaseRole
{    
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\generatedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", unique="TRUE", length=255)
     */
    protected $name;

    /**
     * @ORM\Column(type="string", length=255)
     */
    protected $module;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;

    /**
     * Role class constructor
     */
    public function __construct()
    {
    }

    /**
     * Returns role name.
     * 
     * @return string
     */    
    public function __toString()
    {
        return (string) $this->getName();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Role
     */
    public function setName($name)
    {      
        $name = strtoupper($name);
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set module
     *
     * @param string $module
     * @return Role
     */
    public function setModule($module)
    {
        $this->module = $module;

        return $this;
    }

    /**
     * Get module
     *
     * @return string 
     */
    public function getModule()
    {
        return $this->module;
    }

    /**
     * Set description
     *
     * @param text $description
     * @return Role
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return text 
     */
    public function getDescription()
    {
        return $this->description;
    }
}

2.3 群组类

由于我在群组方面遇到了与角色相同的问题,所以我在这里跳过了它们。如果我能够让角色正常工作,我知道我也可以在群组方面做到同样的事情。

2.4 控制器

namespace Avocode\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\SecurityContext;
use JMS\SecurityExtraBundle\Annotation\Secure;
use Avocode\UserBundle\Entity\User;
use Avocode\UserBundle\Form\Type\UserType;

class UserManagementController extends Controller
{
    /**
     * User create
     * @Secure(roles="ROLE_USER_ADMIN")
     */
    public function createAction(Request $request)
    {      
        $em = $this->getDoctrine()->getEntityManager();

        $user = new User();
        $form = $this->createForm(new UserType(array('password' => true)), $user);

        $roles = $em->getRepository('AvocodeUserBundle:User')
                    ->findAllRolesExceptOwned($user);
        $groups = $em->getRepository('AvocodeUserBundle:User')
                    ->findAllGroupsExceptOwned($user);

        if($request->getMethod() == 'POST' && $request->request->has('save')) {
            $form->bindRequest($request);

            if($form->isValid()) {
                /* Persist, flush and redirect */
                $em->persist($user);
                $em->flush();
                $this->setFlash('avocode_user_success', 'user.flash.user_created');
                $url = $this->container->get('router')->generate('avocode_user_show', array('id' => $user->getId()));

                return new RedirectResponse($url);
            }
        }

        return $this->render('AvocodeUserBundle:UserManagement:create.html.twig', array(
          'form' => $form->createView(),
          'user' => $user,
          'roles' => $roles,
          'groups' => $groups,
        ));
    }
}

2.5 自定义仓库

没有必要发布此内容,因为它们可以正常工作 - 它们返回所有角色/组的子集(未分配给用户的那些)。

2.6 用户类型

用户类型:

namespace Avocode\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class UserType extends AbstractType
{    
    private $options; 

    public function __construct(array $options = null) 
    { 
        $this->options = $options; 
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('username', 'text');

        // password field should be rendered only for CREATE action
        // the same form type will be used for EDIT action
        // thats why its optional

        if($this->options['password'])
        {
          $builder->add('plainpassword', 'repeated', array(
                        'type' => 'text',
                        'options' => array(
                          'attr' => array(
                            'autocomplete' => 'off'
                          ),
                        ),
                        'first_name' => 'input',
                        'second_name' => 'confirm', 
                        'invalid_message' => 'repeated.invalid.password',
                     ));
        }

        $builder->add('email', 'email', array(
                        'trim' => true,
                     ))

        // collection_list is a custom field type
        // extending collection field type
        //
        // the only change is diffrent form name
        // (and a custom collection_list_widget)
        // 
        // in short: it's a collection field with custom form_theme
        // 
                ->add('groups', 'collection_list', array(
                        'type' => new GroupNameType(),
                        'allow_add' => true,
                        'allow_delete' => true,
                        'by_reference' => true,
                        'error_bubbling' => false,
                        'prototype' => true,
                     ))
                ->add('avoRoles', 'collection_list', array(
                        'type' => new RoleNameType(),
                        'allow_add' => true,
                        'allow_delete' => true,
                        'by_reference' => true,
                        'error_bubbling' => false,
                        'prototype' => true,
                     ));
    }

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

    public function getDefaultOptions(array $options){

        $options = array(
          'data_class' => 'Avocode\UserBundle\Entity\User',
        );

        // adding password validation if password field was rendered

        if($this->options['password'])
          $options['validation_groups'][] = 'password';

        return $options;
    }
}

2.7 RoleNameType

此表单旨在呈现以下内容:

  • 隐藏的角色ID
  • 角色名称(只读)
  • 隐藏的模块(只读)
  • 隐藏的描述(只读)
  • 删除(x)按钮

模块和描述呈现为隐藏字段,因为当管理员从用户中删除角色时,该角色应该由jQuery添加到角色表中 - 该表具有模块和描述列。

namespace Avocode\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class RoleNameType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder            
            ->add('', 'button', array(
              'required' => false,
            ))  // custom field type rendering the "x" button

            ->add('id', 'hidden')

            ->add('name', 'label', array(
              'required' => false,
            )) // custom field type rendering <span> item instead of <input> item

            ->add('module', 'hidden', array('read_only' => true))
            ->add('description', 'hidden', array('read_only' => true))
        ;        
    }

    public function getName()
    {
        // no_label is a custom widget that renders field_row without the label

        return 'no_label';
    }

    public function getDefaultOptions(array $options){
        return array('data_class' => 'Avocode\UserBundle\Entity\Role');
    }
}

3. 当前/已知问题

3.1 情况1: 如上所述的配置

以上配置返回错误:

Property "id" is not public in class "Avocode\UserBundle\Entity\Role". Maybe you should create the method "setId()"?

但是ID属性的setter不应该是必需的。

  1. 首先,因为我不想创建一个新角色,我只想在现有的角色和用户实体之间建立关系。
  2. 即使我确实想创建一个新角色,它的ID也应该是自动生成的:

    / **

    • @ORM\Id
    • @ORM\Column(type="integer")
    • @ORM\generatedValue(strategy="AUTO") */ protected $id;

3.2 情况2:在Role实体中添加了ID属性的setter方法

我认为这是错误的,但我这样做只是为了确保。在Role实体中添加了以下代码后:

public function setId($id)
{
    $this->id = $id;
    return $this;
}

如果我创建新用户并添加一个角色,然后保存...会发生以下情况:
  1. 新用户已创建
  2. 新用户被分配了所需ID的角色(太棒了!)
  3. 但该角色的名称被覆盖为空字符串(糟糕!)
显然,这不是我想要的。我不想编辑/覆盖角色。我只想在它们和用户之间建立关系。

3.3 案例3:Jeppe建议的解决方法

当我第一次遇到这个问题时,我最终采用了一个解决方法,与Jeppe建议的相同。今天(因为其他原因),我不得不重新制作我的表单/视图,并且这个解决方法停止工作了。
在Case3 UserManagementController -> createAction中有哪些变化:
  // in createAction
  // instead of $user = new User
  $user = $this->updateUser($request, new User());

  //and below updateUser function


    /**
     * Creates mew iser and sets its properties
     * based on request
     * 
     * @return User Returns configured user
     */
    protected function updateUser($request, $user)
    {
        if($request->getMethod() == 'POST')
        {
          $avo_user = $request->request->get('avo_user');

          /**
           * Setting and adding/removeing groups for user
           */
          $owned_groups = (array_key_exists('groups', $avo_user)) ? $avo_user['groups'] : array();
          foreach($owned_groups as $key => $group) {
            $owned_groups[$key] = $group['id'];
          }

          if(count($owned_groups) > 0)
          {
            $em = $this->getDoctrine()->getEntityManager();
            $groups = $em->getRepository('AvocodeUserBundle:Group')->findById($owned_groups);
            $user->setGroups($groups);
          }

          /**
           * Setting and adding/removeing roles for user
           */
          $owned_roles = (array_key_exists('avoRoles', $avo_user)) ? $avo_user['avoRoles'] : array();
          foreach($owned_roles as $key => $role) {
            $owned_roles[$key] = $role['id'];
          }

          if(count($owned_roles) > 0)
          {
            $em = $this->getDoctrine()->getEntityManager();
            $roles = $em->getRepository('AvocodeUserBundle:Role')->findById($owned_roles);
            $user->setAvoRoles($roles);
          }

          /**
           * Setting other properties
           */
          $user->setUsername($avo_user['username']);
          $user->setEmail($avo_user['email']);

          if($request->request->has('generate_password'))
            $user->setPlainPassword($user->generateRandomPassword());  
        }

        return $user;
    }

很遗憾,这并没有改变什么。结果只有CASE1(没有ID设置器)或CASE2(有ID设置器)。

3.4 情况4:按照用户友好的建议

将cascade = {"persist", "remove"}添加到映射中。

/**
 * @ORM\ManyToMany(targetEntity="Group", cascade={"persist", "remove"})
 * @ORM\JoinTable(name="avo_user_avo_group",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
 * )
 */
protected $groups;

/**
 * @ORM\ManyToMany(targetEntity="Role", cascade={"persist", "remove"})
 * @ORM\JoinTable(name="avo_user_avo_role",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
 * )
 */
protected $avoRoles;

将FormType中的by_reference更改为false

// ...

                ->add('avoRoles', 'collection_list', array(
                        'type' => new RoleNameType(),
                        'allow_add' => true,
                        'allow_delete' => true,
                        'by_reference' => false,
                        'error_bubbling' => false,
                        'prototype' => true,
                     ));

// ...

保留3.3版本中提供的解决方法确实改变了一些东西:

  1. 用户和角色之间的关联没有被创建
  2. 但是角色实体的名称被覆盖为空字符串(就像在3.2版本中一样)

所以,它确实改变了一些东西,但是方向错误。

4. 版本

4.1 Symfony2 v2.0.15

4.2 Doctrine2 v2.1.7

4.3 FOSUserBundle 版本:6fb81861d84d460f1d070ceb8ec180aac841f7fa

5. 总结

我尝试了许多不同的方法(以上只是最近的方法),经过数小时的研究代码、谷歌搜索和寻找答案,我仍然无法使其工作。

非常感谢任何帮助。如果您需要了解任何信息,我会发布您需要的代码部分。


32
+1 是因为这是我长时间以来看到的最详尽和文笔最好的问题之一。 - Jeppe Mariager-Lam
我不喜欢冗长的问题,因为在开始帮助之前,我必须先找到并搜索“问题/疑问”-.- - Jakob Alexander Eichler
这只是EntityRepository的神奇查找器 - 在这里看一下 -> __call()方法 - ioleo
@loostro 你应该开始用英语编写你的PHP注释了。不过问得好。 - fabpico
@FabianPicone 我已经为您快速翻译了这些评论 :) - ioleo
显示剩余3条评论
5个回答

13

我得出了与您相同的结论,即Form组件存在问题,而且我无法找到简单修复它的方法。然而,我想出了一个稍微不那么麻烦的通用解决方案; 它没有任何实体/属性的硬编码知识,因此可以修复它遇到的任何集合:

更简单、通用的解决方法

这不需要您对实体进行任何更改。

use Doctrine\Common\Collections\Collection;
use Symfony\Component\Form\Form;

# In your controller. Or possibly defined within a service if used in many controllers

/**
 * Ensure that any removed items collections actually get removed
 *
 * @param \Symfony\Component\Form\Form $form
 */
protected function cleanupCollections(Form $form)
{
    $children = $form->getChildren();

    foreach ($children as $childForm) {
        $data = $childForm->getData();
        if ($data instanceof Collection) {

            // Get the child form objects and compare the data of each child against the object's current collection
            $proxies = $childForm->getChildren();
            foreach ($proxies as $proxy) {
                $entity = $proxy->getData();
                if (!$data->contains($entity)) {

                    // Entity has been removed from the collection
                    // DELETE THE ENTITY HERE

                    // e.g. doctrine:
                    // $em = $this->getDoctrine()->getEntityManager();
                    // $em->remove($entity);

                }
            }
        }
    }
}

在持久化之前调用新的cleanupCollections()方法

# in your controller action...

if($request->getMethod() == 'POST') {
    $form->bindRequest($request);
    if($form->isValid()) {

        // 'Clean' all collections within the form before persisting
        $this->cleanupCollections($form);

        $em->persist($user);
        $em->flush();

        // further actions. return response...
    }
}

感谢分享。目前我正在处理我的应用程序的不同部分,但当我审查/修订我的应用程序代码时,我一定会尝试您的解决方案。 - ioleo

10
一年过去了,这个问题变得非常流行。Symfony已经发生了改变,我的技能和知识也得到了提高,我的当前解决此问题的方法也得到了改进。
我为Symfony2创建了一组表单扩展(请参见GitHub上的FormExtensionsBundle项目),其中包括处理One/Many ToMany关系的表单类型。
在编写这些代码时,将自定义代码添加到控制器以处理集合是不可接受的 - 表单扩展应该易于使用,开箱即用,并使我们开发人员的生活更加轻松,而不是更难。还要记住.. DRY!
因此,我必须将添加/删除关联代码移动到其他地方 - 而正确的地方自然是EventListener:)
请查看EventListener/CollectionUploadListener.php文件,以了解我们现在如何处理此问题。
PS. 在此复制代码是不必要的,最重要的是像这样的东西实际上应该在EventListener中处理。

8

1. 解决方案

Jeppe Marianger-Lam提出的解决方案是目前我所知道的唯一有效的方法。

1.1 为什么在我的情况下它停止工作了?

我因其他原因更改了我的RoleNameType为:

  • ID(隐藏)
  • name(自定义类型-标签)
  • module&description(隐藏,只读)

问题是我的自定义类型标签将NAME属性呈现为

    <span> role name </span>

由于它不是“只读”的,因此FORM组件期望在POST中获得NAME。

相反,只有ID被POST,因此FORM组件认为NAME为空。

这导致CASE 2(3.2) ->创建关联,但用空字符串覆盖ROLE NAME。

2. 那么,这个解决方案到底是什么?

2.1 控制器

这个解决方案非常简单。

在您的控制器中,在验证表单之前,您必须获取已发布实体标识符并获取匹配的实体,然后将它们设置为您的对象。

// example action
public function createAction(Request $request)
{      
    $em = $this->getDoctrine()->getEntityManager();

    // the workaround code is in updateUser function
    $user = $this->updateUser($request, new User());

    $form = $this->createForm(new UserType(), $user);

    if($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if($form->isValid()) {
            /* Persist, flush and redirect */
            $em->persist($user);
            $em->flush();
            $this->setFlash('avocode_user_success', 'user.flash.user_created');
            $url = $this->container->get('router')->generate('avocode_user_show', array('id' => $user->getId()));

            return new RedirectResponse($url);
        }
    }

    return $this->render('AvocodeUserBundle:UserManagement:create.html.twig', array(
      'form' => $form->createView(),
      'user' => $user,
    ));
}

在updateUser函数中,以下是解决方法的代码:

protected function updateUser($request, $user)
{
    if($request->getMethod() == 'POST')
    {
      // getting POSTed values
      $avo_user = $request->request->get('avo_user');

      // if no roles are posted, then $owned_roles should be an empty array (to avoid errors)
      $owned_roles = (array_key_exists('avoRoles', $avo_user)) ? $avo_user['avoRoles'] : array();

      // foreach posted ROLE, get it's ID
      foreach($owned_roles as $key => $role) {
        $owned_roles[$key] = $role['id'];
      }

      // FIND all roles with matching ID's
      if(count($owned_roles) > 0)
      {
        $em = $this->getDoctrine()->getEntityManager();
        $roles = $em->getRepository('AvocodeUserBundle:Role')->findById($owned_roles);

        // and create association
        $user->setAvoRoles($roles);
      }

    return $user;
}

为了使其起作用,您的SETTER(在此示例中为User.php实体中的SETTER)必须是以下格式:
public function setAvoRoles($avoRoles)
{
    // first - clearing all associations
    // this way if entity was not found in POST
    // then association will be removed

    $this->getAvoRoles()->clear();

    // adding association only for POSTed entities
    foreach($avoRoles as $role) {
        $this->addAvoRole($role);
    }

    return $this;
}

3. 最后的想法

尽管如此,我认为这个解决方法已经完成了工作。

$form->bindRequest($request);

应该这样做!要么是我做错了什么,要么是symfony的集合表单类型不完整。

在symfony 2.1中有一些重大的表单组件变化,希望这个问题能够得到解决。

附注:如果是我做错了什么...

...请告诉我正确的方法!我很乐意看到一个快速、简单和“干净”的解决方案。

附注2:特别感谢:

Jeppe Marianger-Lam和userfriendly(来自IRC上的#symfony2)。你们非常有帮助。干杯!


感谢您发布如此详尽的问题,并跟进您的解决方案,即使它只是一个变通方法。您是否曾经找到了一个“干净”的解决方案来解决这个问题? - RobMasters
不是,但我很快会再次查看这个问题,因为在新的Symfony版本中可能会有所改变 :) - ioleo

6
这是我以前做过的 - 我不知道这是否是“正确”的方法,但它可行。
当您从提交的表单中获取结果时(即在if($form->isValid())之前或之后),只需请求角色列表,然后从实体中删除所有角色(将列表保存为变量)。使用此列表,简单地循环遍历所有角色,请求存储与ID匹配的角色实体,然后在执行persistflush之前将这些实体添加到用户实体中。
我刚刚查阅了Symfony2文档,因为我记得有关于表单集合的原型prototype的信息,然后找到了这个链接:http://symfony.com/doc/current/cookbook/form/form_collections.html。它展示了如何正确处理表单中的javascript添加和删除集合类型的例子。也许先尝试这种方法,如果无法使其正常工作,则可以尝试上面提到的方法 :)

关于JavaScript:我已经阅读了这本食谱,我的JavaScript代码正如预期的那样工作(并且角色正确地发布到控制器)。问题纯粹出现在PHP代码中。这可能与我的表单类型或实体类有关。 - ioleo
你的解决方案就是我两个月前遇到这个问题时所做的。现在由于其他原因,我不得不重新制作我的表单/视图,这个解决方法已经不再适用了。我会发布第三种情况并附上我遇到的错误信息。 - ioleo
谢谢帮忙,解决方法没有奏效,原因我在我的回答中描述了。 - ioleo

0
你需要更多的实体:
用户
id_user(类型:整数)
用户名(类型:文本)
plainPassword(类型:密码)
电子邮件(类型:电子邮件)

分组
id_group(类型:整数)
descripcion(类型:文本)


AVOROLES
id_avorole(类型:整数)
descripcion(类型:文本)


*用户组*
id_user_group(类型:整数)
id_user(类型:整数)(这是用户实体上的ID)
id_group(类型:整数)(这是组实体上的ID)


*USER_AVOROLES*
id_user_avorole (类型:整数)
id_user (类型:整数) (这是用户实体上的ID)
id_avorole (类型:整数) (这是avorole实体上的ID)


你可以像这样设置:
用户:
id:3
用户名:john
明文密码:johnpw
电子邮件:john@email.com
小组:
id_group:5
描述:第5组
用户组:
id_user_group:1
id_user:3
id_group:5
*此用户可以有多个组,因此在另一行中*

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接