Php 将EntityType选项另存为字符串-Symfony 4
我有实体1和实体2 在entity1的表单中,我显示了一个选项列表,其中的选项来自entity2。 我想将所选选项保存为entity1表中列的字符串,但不想在表之间创建任何关系 我该怎么做Php 将EntityType选项另存为字符串-Symfony 4,php,symfony,symfony4,Php,Symfony,Symfony4,我有实体1和实体2 在entity1的表单中,我显示了一个选项列表,其中的选项来自entity2。 我想将所选选项保存为entity1表中列的字符串,但不想在表之间创建任何关系 我该怎么做 class Entity1 { /** * @ORM\Column(type="string") */ private $historico; } class Entity2 { /** * @ORM\Column(type="string") */ private $description; }
class Entity1 {
/**
* @ORM\Column(type="string")
*/
private $historico;
}
class Entity2 {
/**
* @ORM\Column(type="string")
*/
private $description;
}
Entity1FormType.php
$builder->add('historico', EntityType::class, [
'class' => Entity2::class,
'choice_label' => 'description',
'choice_value' => 'description',
'placeholder' => ''
]);
选项显示良好,但提交时出现以下错误:
Expected argument of type "string", "App\Entity\Entity2" given.
如果我使用'mapped'=>false,则输入提交为null
如何将实体对象转换为字符串?
帮助symfony noob:)如果使用mapped=>false,则在提交表单后,必须在控制器中手动获取数据 所以你会有这样的东西:
public function postYourFormAction(Request $request)
{
$entity1 = new Entity1();
$form = $this->createForm(Entity1Type::class $entity1);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$entity1 = $form->getData;
$historico = $form->get('historico')->getData();
$entity1->setHistorico($historico);
$em->persist($entity1);
$em->flush();
}
}
嗨,伙计,我建议你读一下:我想这是你需要的。。。祝你好运!我尝试了这种方法,现在它给了我以下错误:传递给App\Entity\Entity1::setHistorico()的参数1必须是字符串类型,object-given,在第77行的“file/path”中调用,我猜这与之前的错误相同?例如,如果您想在实体中保存historico的“id”:Entity1->setHistorico($historico->getId());或者您可能需要的任何属性啊,我现在就得到了!谢谢Daniele,它工作得非常好。