2015-10-07 41 views
0

我建立用户实体与具有场的角色存储在数据库中的角色:Symfony2的形式收集阵列错误

/** 
    * @var array 
    * @ORM\Column(name="roles", type="json_array") 
    */ 
    private $roles = array(); 

    public function getRoles() 
    { 
     $roles = $this->roles; 
     $roles[] = 'ROLE_USER'; 

     return array_unique($roles); 
    } 

    public function setPassword($password) 
    { 
     $this->password = $password; 

     return $this; 
    } 

它存储与数据库中的一个阵列。这是建立在教程上的。我意识到,如果它像数据库中的数组,它将需要在表单构建器中成为一个集合吗?下面是代码:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('username') 
     ->add('password') 
     ->add('plainPassword', 'repeated', array('type' => 'password', 'required' => false)) 
     ->add('roles', 'choice', array(
      'choices' => array(
       'ROLE_USER' => 'ROLE_USER', 
       'ROLE_ADMIN' => 'ROLE_ADMIN' 
      ), 
      'multiple' => true, 
     )) 
     ->add('isActive') 
     ->add('mail'); 
} 

末树枝渲染

{{ form_widget(edit_form.roles) }} 

所以基本想法是有一个选择栏的所有角色时选择用户,然后更新数据库。但由于某些原因,当我删除

'multiple' => true 

我得到了这样的错误...

enter image description here

什么是这里最好的解决办法?我不想有多种选择,但只有一种选择。

最后一件事是它没有填充数据库,但我得到的信息,当我打电话之前坚持它保存它们,但没有默认。

$entity->setRoles($entity->getRoles()); 

回答

1

您必须改变您的值。

你可以建立一个widget

<?php 

namespace Atix\UserBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 
use Atix\UserBundle\Form\DataTransformer\RolesFormDataTransformer; 

class RolesFormType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $transformer = new RolesFormDataTransformer(); 
     $builder->add('roles', 'choice', array(
      'choices' => array(
          'ROLE_P1'  => 'Role p1', 
          'ROLE_RESPONSABLE' => 'Role responsable', 
          'ROLE_ADMIN'    => 'Role admin', 
          ), 
      'label' => false, 
      'required' => false 
     ))->addModelTransformer($transformer); 

    } 
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     // $resolver->setDefaults(array(
     //  'data_class' => '', 
     //)); 
    } 

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

而且你datatransformer

<?php 

namespace Atix\UserBundle\Form\DataTransformer; 

use Symfony\Component\Form\DataTransformerInterface; 
use Symfony\Component\Form\Exception\TransformationFailedException; 

class RolesFormDataTransformer implements DataTransformerInterface 
{ 
    /** 
    * Transforms an array to a string. 
    * POSSIBLE LOSS OF DATA 
    * 
    * @return string 
    */ 
    public function transform($array) 
    { 
     if (empty($array)) { 
      return $array; 
     } 

     $newArray = array(); 
     $newArray["roles"] = $array; 

     return $newArray; 
    } 

    /** 
    * Transforms a string to an array. 
    * 
    * @param string $string 
    * 
    * @return array 
    */ 
    public function reverseTransform($array) 
    { 

     //var_dump($string); 
     $aRoles = array(); 
     foreach($array as $allValue) 
     { 
      foreach($allValue as $value) 
      { 
       $aRoles[] = $value; 
      } 
     } 
     return $aRoles; 
    } 
} 

您在服务

user.form.type.roles: 
    class: Atix\UserBundle\Form\Type\RolesFormType 
    tags: 
     - { name: form.type, alias: roles_widget } 

您现在可以叫你小部件像这样

声明形式
$builder->add('roles', 'roles_widget'); 
+0

我明白了你的观点。非常感谢! :) –