[英]ChoiceType Not Mapping to Entity Field
我有一個公告實體,將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
,因此映射不會正確發生。
你應該嘗試修復你的吸氣劑:
/**
* @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
來從實體的值中選擇正確的選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.