如何在 Symfony 中多次嵌入一个表单
How to embed a form multiple times in Symfony
我有 2 个实体:Tarifa 和 TarifaPeso(这个实体必须以 Tarifa 形式出现 20 次)。
Tarifa 中的关系是:
/**
* @ORM\OneToMany(targetEntity="TarifaPeso", mappedBy="tarifa", cascade={"persist"})
*/
private $pesos;
我已经按照 Symfony2 文档的说明嵌入了表单,一切正常,但我不想动态地进行,它只能出现 20 次。所以 Tarifa 是一种形式,必须嵌入 20 TarifaPeso 形式。
你知道怎么做吗?谢谢
由于您维护了一个 @ORM\OneToMany
关联,Collection Field Type 应该可以解决您的目的。在生成表单之前,您只需将 TarifaPeso
的 20 个实例与 Tarifa
实体相关联。
See here 如何嵌入 collection 表单类型。
确保您已从 Tarifa
实体启用 cascade-persist
自动 insert
或 update
TarifaPeso
个实体。
根据我对 Collection
字段类型的经验。这在数据完整性方面会更快更容易。
最后,我在父表单的构造函数中完成了此操作 class(在实体中):
$this->envios = new \Doctrine\Common\Collections\ArrayCollection();
for ($i = 0; $i < count(self::$KILOS); $i++) {
$peso = new TarifaPeso();
$peso->setKilosMaxlim(self::$KILOS[$i]);
$this->addPeso($peso);
}
体重 $千:
private static $KILOS = array(1,2,3,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,300,500,1000,1001);
这样我用eventListener修改了表单:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('precio', null, array(
'attr' => array('autofocus' => true),
'label' => 'label.precio ' ,
));
;
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
if ($data) {
$valor = $data->getKilosMaxlim();
$field = $form->get('precio');
$config = $field->getConfig();
$options = $config->getOptions();
$options['label'] = 'Valor para '.$valor; // change the label
$form->add($field->getName(), $config->getType()->getName(), $options);
}
});
}
我有 2 个实体:Tarifa 和 TarifaPeso(这个实体必须以 Tarifa 形式出现 20 次)。
Tarifa 中的关系是:
/**
* @ORM\OneToMany(targetEntity="TarifaPeso", mappedBy="tarifa", cascade={"persist"})
*/
private $pesos;
我已经按照 Symfony2 文档的说明嵌入了表单,一切正常,但我不想动态地进行,它只能出现 20 次。所以 Tarifa 是一种形式,必须嵌入 20 TarifaPeso 形式。
你知道怎么做吗?谢谢
由于您维护了一个 @ORM\OneToMany
关联,Collection Field Type 应该可以解决您的目的。在生成表单之前,您只需将 TarifaPeso
的 20 个实例与 Tarifa
实体相关联。
See here 如何嵌入 collection 表单类型。
确保您已从 Tarifa
实体启用 cascade-persist
自动 insert
或 update
TarifaPeso
个实体。
根据我对 Collection
字段类型的经验。这在数据完整性方面会更快更容易。
最后,我在父表单的构造函数中完成了此操作 class(在实体中):
$this->envios = new \Doctrine\Common\Collections\ArrayCollection();
for ($i = 0; $i < count(self::$KILOS); $i++) {
$peso = new TarifaPeso();
$peso->setKilosMaxlim(self::$KILOS[$i]);
$this->addPeso($peso);
}
体重 $千:
private static $KILOS = array(1,2,3,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,300,500,1000,1001);
这样我用eventListener修改了表单:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('precio', null, array(
'attr' => array('autofocus' => true),
'label' => 'label.precio ' ,
));
;
$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
if ($data) {
$valor = $data->getKilosMaxlim();
$field = $form->get('precio');
$config = $field->getConfig();
$options = $config->getOptions();
$options['label'] = 'Valor para '.$valor; // change the label
$form->add($field->getName(), $config->getType()->getName(), $options);
}
});
}