gpt4 book ai didi

php - Symfony 2 嵌入式表单集合多对多

转载 作者:可可西里 更新时间:2023-10-31 23:08:49 27 4
gpt4 key购买 nike

我有 2 个实体 - 用户和组。它们是多对多的关系,Group用于存储用户的角色。

我正在尝试通过添加一个集合来创建一个用户编辑表单,我希望能够通过从下拉列表中选择它来添加一个新角色(仅限于数据库中已有的角色)

用户类型.php:

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username')
->add('email')
->add('forename')
->add('surname')
->add('isActive')
->add('joinDate', 'date', array('input' => 'datetime', 'format' => 'dd-MM-yyyy'))
->add('lastActive', 'date', array('input' => 'datetime', 'format' => 'dd-MM-yyyy'))
->add('groups', 'collection', array(
'type' => new GroupType(),
'allow_add' => true,
))
;
}

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

和GroupType.php:

class GroupType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('role');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
"data_class" => 'Sfox\CoreBundle\Entity\Group'
));
}
}

这会在基本文本框中显示表单中的角色,但如果我向表单添加一个条目,它会将新条目级联保存到组中,如果我要编辑一个条目,它会更改基础组数据.

我尝试制作一个 GroupSelectType.php:

class GroupSelectType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('role', 'entity', array('class'=>'SfoxCoreBundle:Group', 'property'=>'name'));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
"data_class" => 'Sfox\CoreBundle\Entity\Group'
));
}
}

将字段添加为“实体”类型,这会显示正确的选择框(但使用默认值)我似乎无法将其绑定(bind)到 UserType 表单!

我想让表单做的只是修改用户实体中的底层“组”ArrayCollection。

有谁知道我怎样才能做到这一点?

最佳答案

好吧,我为遇到类似问题的其他人制定了解决方案...

我必须创建自定义表单类型并将其声明为服务,这样我才能传入实体管理器。然后我需要制作一个 dataTransformer 以将我的组对象更改为表单的整数

自定义组选择类型:

class GroupSelectType extends AbstractType

{
/**
* @var ObjectManager
*/
private $om;

private $choices;

/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;

// Build our choices array from the database
$groups = $om->getRepository('SfoxCoreBundle:Group')->findAll();
foreach ($groups as $group)
{
// choices[key] = label
$this->choices[$group->getId()] = $group->getName() . " [". $group->getRole() ."]";
}
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new GroupToNumberTransformer($this->om);
$builder->addModelTransformer($transformer);
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
"choices" => $this->choices,
));
}

public function getParent()
{
return 'choice';
}

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

在构造函数中,我获取所有可用的组并将它们放入一个“选择”数组中,该数组作为一个选项传递给选择框。

您还会注意到我正在使用自定义数据转换器,这是为了将 groupId(用于呈现表单)更改为组实体。我也将 GroupSelectType 设为服务并传入 [@doctrine.orm.entity_manager]

services.yml(捆绑配置):

services:
sfox_core.type.group_select:
class: Sfox\CoreBundle\Form\Type\GroupSelectType
arguments: [@doctrine.orm.entity_manager]
tags:
- { name: form.type, alias: group_select }

GroupToNumberTranformer.php

class GroupToNumberTransformer implements DataTransformerInterface
{
/**
* @var ObjectManager
*/
private $om;

/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om)
{
$this->om = $om;
}

/**
* Transforms an object (group) to a string (number).
*
* @param Group|null $group
* @return string
*/
public function transform($group)
{
if (null === $group) {
return "";
}

return $group->getId();
}

/**
* Transforms a string (number) to an object (group).
*
* @param string $number
* @return Group|null
* @throws TransformationFailedException if object (group) is not found.
*/
public function reverseTransform($number)
{
if (!$number) {
return null;
}

$group = $this->om
->getRepository('SfoxCoreBundle:Group')
->findOneBy(array('id' => $number))
;

if (null === $group) {
throw new TransformationFailedException(sprintf(
'Group with ID "%s" does not exist!',
$number
));
}

return $group;
}
}

我修改过的 UserType.php - 注意我正在使用我的自定义表单类型“group_select”,因为它作为服务运行:

class UserType extends AbstractType
{
private $entityManager;

public function __construct($entityManager)
{
$this->entityManager = $entityManager;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new GroupToNumberTransformer($this->entityManager);

$builder
->add('username')
->add('email')
->add('forename')
->add('surname')
->add('isActive')
->add('joinDate', 'date', array('input' => 'datetime', 'format' => 'dd-MM-yyyy'))
->add('lastActive', 'date', array('input' => 'datetime', 'format' => 'dd-MM-yyyy'));
$builder
->add(
$builder->create('groups', 'collection', array(
'type' => 'group_select',
'allow_add' => true,
'options' => array(
'multiple' => false,
'expanded' => false,
)
))
);
}

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

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

关于php - Symfony 2 嵌入式表单集合多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12730544/

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