Php ChoiceType未映射到实体字段
我有一个公告实体,其中映射了Php ChoiceType未映射到实体字段,php,symfony,symfony-forms,Php,Symfony,Symfony Forms,我有一个公告实体,其中映射了EditAnnouncementType表单。我还有另外两个CheckBoxType表单自动更新各自的字段,但是ChoiceType表单不起作用 $builder ->add('edit', SubmitType::class, array ( 'label' => 'Save changes', 'attr' => ['c
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返回一个
字符串
,因此映射不会正确发生
您应该尝试修复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带来问题,特别是如果它使用
数组_值
从实体的值中选择正确的选项 修复了getter返回int但仍然不起作用的问题:那么重复的值3呢?(参见编辑后的答案)接受这个答案,尽管我无法准确地复制它开始工作的原因。在发布这个问题到现在之间的某个时候,我将映射
更改为错误
。将其更改回true
修复了它。字符串返回并不重要