역할 엔티티와 다 대다 관계가있는 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';
}
}
http://docs.doctrine-project.org/ko/2.0.x/reference/association-mapping.html#many-to-many-bidirectional 양방향 ManyToMany에는'mappedBy'가 있어야하며'inversedBy'가 두 개가 아니어야합니다. – juanmf