질문은 this one과 비슷하지만 큰 차이가 있습니다. 추가 할 고정 된 수의 요소가 없습니다.Symfony 2.3.6. 중첩 된 양식
아래는 양식의 미리보기와 모양을 보여줍니다. 나는 사용자 엔티티에 대해 서로 다른 응용 프로그램 엔티티을 포함하고 각 응용 프로그램 엔티티는 여러 개의 숫자 사용자 그룹 엔티티을 포함합니다.
사용자
class User extends BaseUser
{
...
/**
* @ORM\ManyToMany(targetEntity="Application", inversedBy="users")
* @ORM\JoinTable(name="users_applications")
*/
protected $applications;
/**
* @ORM\ManyToMany(targetEntity="UserGroup", inversedBy="users")
* @ORM\JoinTable(name="users_groups")
*/
protected $user_groups;
응용 프로그램
class Application
{
...
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="applications")
*/
protected $users;
/**
* @ORM\OneToMany(targetEntity="UserGroup", mappedBy="application")
*/
protected $user_groups;
사용자 그룹
,class UserGroup
{
...
/**
* @ORM\ManyToOne(targetEntity="Application", inversedBy="user_groups")
* @ORM\JoinColumn(name="application_id", referencedColumnName="id")
*/
protected $application;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="user_groups")
*/
protected $users;
UserFormType
class UserFormType extends AbstractType
{
// Array of applications is generated in the Controller and passed over by the constructor
private $applications;
public function buildForm(FormBuilderInterface $builder, array $options)
{
...
if ($this->applications && count($this->applications) > 0)
{
foreach ($this->applications AS $application)
{
$builder->add('applications', 'entity', array
(
'class' => 'MyBundle:Application',
'property' => 'title',
'query_builder' => function(EntityRepository $er) use ($application)
{
return $er->createQueryBuilder('a')
->where('a.id = :id')
->setParameter('id', $application->getId());
},
'expanded' => true,
'multiple' => true
));
$builder->add('user_groups', 'entity', array
(
'class' => 'MyBundle:UserGroup',
'property' => 'title',
'query_builder' => function(EntityRepository $er) use ($application)
{
return $er->createQueryBuilder('ug')
->where('ug.application = :application')
->setParameter('application', $application);
},
'expanded' => true,
'multiple' => true
));
}
}
...
문제 : 나는 이미 응용 프로그램 및 사용자 그룹 엔티티을 포함하도록 관리해야하지만, 응용 프로그램 엔티티 이후는에 의해 formbuilder에 추가 루프를 사용하면 엔티티를 덮어 써서 여러 응용 프로그램을 하나의 응용 프로그램으로 만 렌더링 할 수 있습니다.
우리는 중첩 된 양식을위한 일종의 simmilar 설정이 있지만 콜렉션을 사용합니다. 아마도 여러분의 문제에 대한 해결책이 될 수 있습니다! http://symfony.com/doc/current/cookbook/form/form_collections.html – acrobat
반복 할 때마다 동일한 필드를 추가하기 위해 노력하고 있습니다 (응용 프로그램 이름 지정). 불가능합니다. 엔티티 부속 유형을 사용하여이 필드의 특수 유형을 작성하는 것이 좋습니다 – zulus