3

質問はこれと似ていますが、大きな違いがあります。追加したい要素の数が決まっていません。

その下には、フォームのプレビューとその外観が表示されます。さまざまなアプリケーション エンティティを含むユーザー エンティティのフォームがあり、各アプリケーション エンティティには複数のユーザー グループ エンティティがあります。

私のフォームがどのように見えるべきか - ネストされたエンティティ

ユーザー

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;

ユーザーフォームの種類

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

問題:アプリケーションエンティティとユーザー グループ エンティティを含めることはできましたが、ループによってアプリケーション エンティティがフォームビルダーに追加されるため、エンティティが上書きされ、複数のアプリケーションが存在する場合、レンダリングされるアプリケーションは 1 つだけになります。

4

1 に答える 1