我有一个公告实体,其中EditAnnouncementType
表格被映射到。我有两个其他CheckBoxType
表单自动更新其各自的字段,但ChoiceType
表单不起作用。
$builder
->add('edit', SubmitType::class,
array
(
'label' => 'Save changes',
'attr' => ['class' => 'btn btn-primary']
))
->add('type', ChoiceType::class,
[
'choices' => [
'info_type' => 1,
'star_type' => 2,
'alert_type' => 3,
'lightbulb_type' => 3,
'event_type' => 4,
'statement_type' => 5,
'cat_type' => 6,
'hands_type' => 7
],
'mapped' => true,
'expanded' => true,
'required' => true,
])
公告实体和类型字段
class Announcement
{
/**
* @var int
*/
private $type;
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @param int $type
*
* @return $this
*/
public function setType(int $type)
{
$this->type = $type;
return $this;
}
我怀疑Symfony会以某种方式严格检查价值(使用===
)。
由于你的getter返回一个string
,映射不会正确发生。
你应该尝试修复你的getter:
/**
* @return int
*/
public function getType(): int
{
return $this->type;
}
还要注意您的选择数组可能有问题:
// be careful: those two have the same value
'alert_type' => 3,
'lightbulb_type' => 3,
这肯定会给Symfony带来问题,特别是如果它使用array_values
来从你的实体的价值中选择正确的选择。