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

iol*_*leo 68 php forms collections entity symfony

1.快速概述

1.1目标

我想要实现的是创建/编辑用户工具.可编辑的字段是:

  • 用户名(类型:文字)
  • plainPassword(类型:密码)
  • 电邮(类型:电邮)
  • 组(类型:集合)
  • avoRoles(类型:集合)

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

1.2用户界面

我的模板由两部分组成:

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

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

1.3所需功能

1.3.1添加角色:


    WHEN user clicks "+" (add) button in Roles table  
    THEN jquery removes that row from Roles table  
    AND  jquery adds new list item to User form (avoRoles list)

1.3.2删除角色:


    WHEN user clicks "x" (remove) button in  User form (avoRoles list)  
    THEN jquery removes that list item from User form (avoRoles list)  
    AND  jquery adds new row to Roles table

1.3.3保存更改:


    WHEN user clicks "Zapisz" (save) button  
    THEN user form submits all fields (username, password, email, avoRoles, groups)  
    AND  saves avoRoles as an ArrayCollection of Role entities (ManyToMany relation)  
    AND  saves groups as an ArrayCollection of Role entities (ManyToMany relation)  

注意:只能将现有角色和组分配给用户.如果由于任何原因找不到,表格不应该验证.


2.代码

在本节中,我将介绍/或简要介绍此操作背后的代码.如果描述不够,你需要看到代码告诉我,我会粘贴它.我不是首先将它全部粘贴,以避免使用不必要的代码向您发送垃圾邮件.

2.1用户类

我的User类扩展了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;
    }
}
Run Code Online (Sandbox Code Playgroud)

2.2角色类

我的Role类扩展了Symfony Security Component Core Role类.

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

2.3小组课程

Since I've got the same problem with groups as with roles, I'm skipping them here. If I get roles working I know I can do the same with groups.

2.4 Controller

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,
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

2.5 Custom repositories

It is not neccesary to post this since they work just fine - they return a subset of all Roles/Groups (those not assigned to user).

2.6 UserType

UserType:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

2.7 RoleNameType

This form is supposed to render:

  • hidden Role ID
  • Role name (READ ONLY)
  • hidden module (READ ONLY)
  • hidden description (READ ONLY)
  • remove (x) button

Module and description are rendered as hidden fields, becouse when Admin removes a role from a User, that role should be added by jQuery to Roles Table - and this table has Module and Description columns.

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

3. Current/known Problems

3.1 Case 1: configuration as quoted above

The above configuration returns error:

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

But setter for ID should not be required.

  1. First becouse I don't want to create a NEW role. I want just to create a relation between existing Role and User entities.
  2. Even if I did want to create a new Role, it's ID should be auto-generated:

    /**

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

3.2 Case 2: added setter for ID property in Role entity

I think it's wrong, but I did it just to be sure. After adding this code to Role entity:

public function setId($id)
{
    $this->id = $id;
    return $this;
}
Run Code Online (Sandbox Code Playgroud)

If I create new user and add a role, then SAVE... What happens is:

  1. New user is created
  2. New user has role with the desired ID assigned (yay!)
  3. but that role's name is overwritten with empty string (bummer!)

Obviously, thats not what I want. I don't want to edit/overwrite roles. I just want to add a relation between them and the User.

3.3 Case 3: Workaround suggested by Jeppe

When I first encountered this problem I ended up with a workaround, the same that Jeppe suggested. Today (for other reasons) I had to remake my form/view and the workaround stopped working.

What changes in 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;
    }
Run Code Online (Sandbox Code Playgroud)

Unfortunately this does not change anything.. the results are either CASE1 (with no ID setter) or CASE2 (with ID setter).

3.4 Case 4: as suggested by userfriendly

Adding cascade={"persist", "remove"} to mapping.

/**
 * @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;
Run Code Online (Sandbox Code Playgroud)

And changeing by_reference to false in FormType:

// ...

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

// ...
Run Code Online (Sandbox Code Playgroud)

And keeping workaround code suggested in 3.3 did change something:

  1. Association between user and role was not created
  2. .. but Role entity's name was overwritten by empty string (like in 3.2)

So.. it did change something but in the wrong direction.

4. Versions

4.1 Symfony2 v2.0.15

4.2 Doctrine2 v2.1.7

4.3 FOSUserBundle version: 6fb81861d84d460f1d070ceb8ec180aac841f7fa

5. Summary

I've tried many diffrent approaches (above are only the most recent ones) and after hours spent on studying code, google'ing and looking for the answer I just couldn't get this working.

Any help will be greatly appreciated. If you need to know anything I'll post whatever part of code you need.

Rob*_*ers 13

我得出了相同的结论,表单组件有问题,无法找到一种简单的方法来解决它.但是,我提出了一个完全通用的稍微麻烦的解决方案; 它没有任何实体/属性的硬编码知识,因此将修复它遇到的任何集合:

更简单,通用的解决方法

这不要求您对实体进行任何更改.

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);

                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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...
    }
}
Run Code Online (Sandbox Code Playgroud)


iol*_*leo 10

所以一年过去了,这个问题已经变得非常流行.Symfony已经发生了变化,我的技能和知识也得到了改善,我目前解决这个问题的方法也是如此.

我为symfony2创建了一组表单扩展(请参阅github上的FormExtensionsBundle项目),它们包含一个用于处理One/Many ToMany关系的表单类型.

在编写这些内容时,向控制器添加自定义代码来处理集合是不可接受的 - 表单扩展应该易于使用,开箱即用,让开发人员的生活更轻松,而不是更难.还..记得..干!

所以我不得不在其他地方移动添加/删除关联代码 - 而正确的地方当然是一个EventListener :)

看看EventListener/CollectionUploadListener.php文件,看看我们现在如何处理它.

PS.在这里复制代码是不必要的,最重要的是这样的东西应该在EventListener中实际处理.


iol*_*leo 8

1.解决方案

Jeppe Marianger-Lam建议的解决方案解决方案目前是我所知道的唯一一个解决方案.

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

我将RoleNameType(由于其他原因)更改为:

  • ID(隐藏)
  • 名称(自定义类型 - 标签)
  • 模块和描述(隐藏,只读)

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


    <span> role name </span>

由于它不是"只读",因此FORM组件可能会在POST中获得NAME.

相反,只有ID被POST,因此FORM组件假定NAME为NULL.

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

那么,这个解决方法究竟是什么呢?

2.1控制器

这种解决方法非常简单.

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

// 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,
    ));
}
Run Code Online (Sandbox Code Playgroud)

并在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;
}
Run Code Online (Sandbox Code Playgroud)

为此,您的SETTER(在本例中为User.php实体)必须是:

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;
}
Run Code Online (Sandbox Code Playgroud)

3.最后的想法

不过,我认为这种解决方法正在完成这项工作

$form->bindRequest($request);
Run Code Online (Sandbox Code Playgroud)

应该做!这是我做错了,或者symfony的Collection表单类型不完整.

在symfony 2.1中,Form组件有一些重大变化,希望这将得到修复.

PS.如果是我做错了什么......

...请按照应该的方式发布!我很高兴看到一个快速,简单和"干净"的解决方案.

PS2.特别感谢:

Jeppe Marianger-Lam和用户友好(来自IRC上的#symfony2).你一直非常乐于助人.干杯!


Jep*_*Lam 6

这就是我之前所做的 - 我不知道这是否是"正确"的方式,但它确实有效.

当您从提交的表单中获得结果时(即,在之前或之后if($form->isValid())),只需询问角色列表,然后从实体中删除它们(将列表另存为变量).使用此列表,只需遍历它们,向存储库询问与ID匹配的角色实体,并在您persist和之前将这些添加到您的用户实体flush.

我刚刚搜索了Symfony2文档,因为我记得有关prototype表单集的一些内容,并且出现了这个问题:http://symfony.com/doc/current/cookbook/form/form_collections.html - 它提供了如何正确处理的示例javascript在表单中添加和删除集合类型.或许首先尝试这种方法,然后尝试我之后提到的如果你不能让它工作:)