如何同时使用两个实体

时间:2017-09-21 09:12:11

标签: symfony symfony-forms

我有两个实体calcPara calcSet

我想用这个表格制作一个表格。

我可以像这些一样制作每个表格

$calcPara = new CalcPara();
$form = $this->createFormBuilder($calcPara)->add('save', SubmitType::class)
    ->getForm();


$calcSet = new CalcSet();
$form = $this->createFormBuilder($calcSet)->add('save', SubmitType::class)
    ->getForm();

然而,这有两种不同的形式。

但我想从两个实体制作一个表格。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

在Symfony中,Form有一个对象作为支持它的数据,因此您不能直接指定两个实体来扮演该角色。如果没有完全手动构建表单,您可以做一些准备工作,将两个实体组合成一个新类型,您定义的新类型包含来自两个实体的重要成员。但是,您必须负责将两个Entites转换为新对象,并在提交表单时再次返回。

e.g。

class CalcSetAndPara {

    public function setSetValue($setValue) {}
    public function getSetValue() {}
    public function setParaValue($paraValue) {}
    public function getParaValue(){}
}

使用它:

$combinedObject = new CalcSetAndPara();
$combinedObject->setSetValue($calcSet->getValue());
$combinedObject->setParaValue($calcPara->getValue());

$form = $this->createFormBuilder($combinedObject)->add('save', SubmitType::class)
    ->getForm();

//Then handle and do whatever you need to do with the results, extracting and persisting the two entities
相关问题