gpt4 book ai didi

forms - 交响乐 2.3.6。嵌套形式

转载 作者:行者123 更新时间:2023-12-04 02:42:40 25 4
gpt4 key购买 nike

问题类似于this one ,但有一个巨大的区别:我没有固定数量的要添加的元素。

下面是表单的预览及其外观。我有一个用户实体的表单,其中包含不同的应用程序实体,每个应用程序实体都有多个用户组实体

The way my form should look like - nested entities

用户

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

问题:我已经设法包含应用程序用户组实体,但是由于应用程序实体> 通过循环添加到 formbuilder,实体被覆盖,这样有多个应用程序只有一个应用程序被渲染。

最佳答案

你确实想要收藏品。集合允许任意数量的元素(满足您的要求)。我认为问题是您应该有一个 UserGroupType 以便集合按您想要的方式工作。

这里或多或少是您的表单类型的完整示例。省略了命名空间和其他内容。

Controller(记住,您正在将 User 对象传递给表单,不要执行您在 private $applications 中所做的事情):

...
$user = ...;
$form = $this->createForm(new UserFormType(), $user);

对 UserFormType 的更改:

class UserFormType extends AbstractType
{
// Don't have that $applications member here...

public function buildForm(FormBuilderInterface $builder, array $options)
{
//...

// Your user has many UserGroups, so display each one as a UserGroup field (see UserGroupFormType)
$builder->add('user_groups', 'collection', array(
'type' => new UserGroupFormType() // I would make this type a service (http://symfony.com/doc/current/book/forms.html#defining-your-forms-as-services)
));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Namespace\Entity\User'
));
}

// ... getname, etc.
}

新用户组表单类型:

class UserGroupFormType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
// Each UserGroup has 1 application and multiple users
// The entity fields allow you to select these

$builder->add('application', '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' => false
));

$builder->add('users', 'entity', array
(
'class' => 'MyBundle:User',
'property' => 'title',
'query_builder' => function(EntityRepository $er) use ($application)
{
return $er->createQueryBuilder('ug')
->where('ug.application = :application')
->setParameter('application', $application);
},
'expanded' => true,
'multiple' => true
));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Namespace\Entity\UserGroup'
));
}

// ... getname, etc.
}

另见 http://symfony.com/doc/current/cookbook/form/form_collections.html这向您展示了如何在表单中添加新的用户组(如果您需要该功能)。

关于forms - 交响乐 2.3.6。嵌套形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19561394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com