gpt4 book ai didi

Symfony 表单 : how to include in the Doctrine entity form a form for a Doctrine embeddable used by the entity itself

转载 作者:行者123 更新时间:2023-12-02 17:29:55 27 4
gpt4 key购买 nike

我有一个使用 Doctrine embeddable 的 Doctrine Entity .

我想创建一个表单来编辑 EntityEmbeddable

我的实体:

/**
* MyEntity
*
* @ORM\Table(name="my_entities")
* @ORM\Entity(repositoryClass="\AppBundle\Entity\MyEntityRepository")
* @ORM\HasLifecycleCallbacks
*/
class MyEntity
{
...

/**
* @var MyEmbeddableInfo
*
* @ORM\Embedded(class="AppBundle\Entity\Embeddable\MyInfoEmbeddable")
*/
private $myInfo;

...

我的可嵌入:

/**
* @ORM\Embeddable
*/
class MyInfoEmbeddable
{
/**
* @var string
*
* @ORM\Column(name="info1", type="string", nullable=true)
*
* @Assert\NotBlank(groups={"setUp"})
*/
private $info1;

/**
* @var string
*
* @ORM\Column(name="info2", type="string", nullable=true)
*
* @Assert\NotBlank(groups={"setUp"})
*/
private $info2;

/**
* @var
*
* @ORM\Column(name="info3", type="string", nullable=true)
*
* @Assert\NotBlank(groups={"setUp"})
*/
private $info3;

...

我尝试了什么

我找到了这个 question here on Stackoverflow似乎用户遇到了同样的问题,但不同的是我从来没有收到 Warning,因为我根本不理解为可嵌入对象创建表单的基本过程。

我进行了两项测试:一项是将可嵌入对象直接传递给 MyEntityType 表单生成器,一项是创建 MyInfoType() 表单类型。

测试 1:直接传递可嵌入对象

这是我的MyEntityType类的代码:

class MyEntityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
...
->add('myInfo', 'entity', ['class' => '\AppBundle\Entity\Embeddable\MyInfoEmbeddable'])
...
}

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

这个解决方案抛出一个RuntimeException:

Class \AppBundle\Entity\Embeddable\MyInfoEmbeddable seems not to be a managed Doctrine entity. Did you forget to map it?

测试 2:传递 MyInfoType 对象

我做的第二个测试是使用表单类型(如 linked SO question 中所建议):

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
...
->add('myInfo', new MyInfoType())
...
}

这是 MyInfoType 的代码:

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('infoField1', 'text', ['required' => false])
->add('infoField2', 'text', ['required' => false])
->add('infoField3', 'choice', [
'choices' => [
'' => '',
'choice1' => 'Choice 1',
'choice2' => 'Choice 2',
'choice3' => 'Choice 3'
],
'required' => false
]
);
}

相反,这会返回一个 Symfony\Component\PropertyAccess\Exception\NoSuchIndexException:

Cannot read index infoField1 from object of type AppBundle\Entity\Embeddable\MyInfoEmbeddable because it doesn't implement \ArrayAccess.

所以,这个解决方案也行不通。

我的目标是为 MyEntity 创建一个表单,并在该表单中创建用于更新 MyInfoEmbeddable 的字段。

要遵循的正确程序是什么?

最佳答案

而不是在你的主窗体中:

$builder->add('myInfo', new MyInfoType(), 
['data_class' => '\AppBundle\Entity\Embeddable\MyInfoEmbeddable'])

您可以将此添加到您的 MyInfoType 表单中(我相信它与可嵌入实体链接):

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => '\AppBundle\Entity\Embeddable\MyInfoEmbeddable',
]);
}

然后在您的主表单中简单地调用可嵌入表单类型,如下所示:

$builder->add('myInfo', new MyInfoType())

关于Symfony 表单 : how to include in the Doctrine entity form a form for a Doctrine embeddable used by the entity itself,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34962439/

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