2013-11-04 3 views
3

역할 엔티티와 다 대다 관계가있는 Doctrine 사용자 엔티티가 있습니다. 또한 사용자 엔티티 필드를 기반으로 사용자 세부 정보를 표시하는 양식이 있습니다. 양식 작성기를 사용하는 컨트롤러에서 정상적인 방법으로 양식을 설정하고 데이터베이스에서로드 된 엔터티 인스턴스를 전달합니다. 이 모든 것이 잘 작동합니다.다 대다 관계가있는 Symfony2 양식 엔티티 필드

이제 사용자가 선택한 역할이있는이 양식의 선택 메뉴가 선택되어 데이터베이스의 사용 가능한 역할에서 채워집니다. UserType 양식을 roles이라는 형식으로 만들고 '컬렉션'유형을 가지고 RoleType 양식 인스턴스로 전달했습니다. 내 RoleType 양식에서는 entity 유형의 필드를 추가하고 내 역할 클래스 등을 정의합니다.이 모든 것은 설명서에 따른 것입니다. 이 모든 것은 잘 작동하지만 역할로 채워진 선택 메뉴를로드하지만 사용자 엔티티에 대해 저장된 올바른 역할은 선택하지 않습니다.

'roles'의 양식 값을 추적하거나 내 역할 엔티티 필드에 데이터 변환기를 설정하면 사용자가 연결되는 역할의 이름이 포함 된 문자열이 표시됩니다. 나는 Role 인스턴스 나 Collection/Array를 얻지 못한다. 또한 역할 엔터티 필드를 multiple = true으로 설정하면 Doctrine 데이터 변환기에서 Expected a Doctrine\Common\Collections\Collection object. 오류가 발생합니다. 다시 말하지만, 컬렉션을 기대하고 문자열을 얻기 때문입니다.

내 사용자 엔티티가 수화되는 방식과 관련이 있습니다. 나는 $repository->findOneBy(array('id' => $id))을 사용하고있다;

이의 단순화 된 버전이 무엇인지 내가 뭐하는 거지 :

사용자 클래스

class User implements AdvancedUserInterface, \Serializable 
{ 
    /** 
    * @ORM\ManyToMany(targetEntity="Role", inversedBy="users") 
    */ 
    public $roles; 

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

    public function getRoles() 
    { 
     return $this->roles->toArray(); 
    } 
} 

역할 클래스

class Role implements RoleInterface 
{ 
    /** 
    * @ORM\ManyToMany(targetEntity="User", inversedBy="roles") 
    */ 
    public $users; 

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

    public function getUsers() 
    { 
     return $this->users; 
    } 
} 

사용자 양식 유형

class UserType extends AbstractType 
{ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'NameSpace\MyBundle\Entity\User', 
     )); 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('id', 'hidden') 
      ->add('roles', 'collection', array('type' => new RoleType())) 
      ->add('save', 'submit'); 
    } 

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

0 역할 양식 유형

class RoleType extends AbstractType 
{ 
    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'NameSpace\MyBundle\Entity\Role', 
     )); 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('name', 'entity', array(
      'class' => 'NameSpace\MyBundle\Entity\Role', 
      'property' => 'name' 
      [multiple => true] 
     )); 
    } 

    public function getName() 
    { 
     return 'role'; 
    } 
} 
+0

http://docs.doctrine-project.org/ko/2.0.x/reference/association-mapping.html#many-to-many-bidirectional 양방향 ManyToMany에는'mappedBy'가 있어야하며'inversedBy'가 두 개가 아니어야합니다. – juanmf

답변

4

당신은 역할에 대한 사용자 개체에서 폼에 컬렉션을 제공되지 않습니다

public function getRoles() 
{ 
    //this returns an array 
    return $this->roles->toArray(); 
} 

양식 클래스에서 getter 및 setter를 사용 엔티티가 실제로 배열을 반환하므로 Symfony2 보안 시스템에 필요한 것입니다.

public function getRolesCollection() 
{ 
    //this returns a collection 
    return $this->roles; 
} 

//and (updated from comment below) 
->add('roles_collection', 'entity', array('type' => new RoleType())) 

오로 플랫폼은 같은 것을 할 : https://github.com/orocrm/platform/blob/master/src/Oro/Bundle/UserBundle/Form/Type/UserType.php

이 블로그 게시물도 유용 할 수 있습니다 http://blog.jmoz.co.uk/symfony2-fosuserbundle-role-entities/ 그것은 무엇을 당신이해야 할 것은 getRolesCollection 필드를 구현하고 대신 형태로 그 사용도 FOSUserBundle의 데이터베이스에 역할을 추가하는 사람입니다

+0

좋아, 고마워. 그것은 문제의 일부였습니다. 다른 부분은 UserType 형식의 'collection'대신 'roles_collection'필드 유형을 'entity'로 만들어야했습니다. 이로써 'RoleType'양식 클래스의 필요성이 제거되었습니다. 이것은 게시 한 첫 번째 링크에 설명되어 있습니다. – Jon

+0

멋지다, 내 대답을 – Luke

+0

업데이트했습니다. getRolesCollection()'addRoleCollection ($ role)'removeRoleCollection ($ role)'을 [get | add | remove] Role() 프록시를 추가하는 것으로 변경했습니다. – juanmf

관련 문제