简体   繁体   English

ChoiceType 未映射到实体字段

[英]ChoiceType Not Mapping to Entity Field

I have an Announcement Entity that where a EditAnnouncementType form is mapped to.我有一个公告实体,将EditAnnouncementType表单映射到该实体。 I have two other CheckBoxType forms that automatically update their respective fields, but the ChoiceType form isn't working.我还有另外两个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,
            ])

The Announcement entity and type field公告实体和类型字段

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;
    }

My suspicion would be that Symfony somehow strict checks the value (using === ).我怀疑 Symfony 以某种方式严格检查值(使用=== )。
And since your getter returns a string , the mapping doesn't happen properly.并且由于您的 getter 返回一个string ,因此映射不会正确发生。

You should try fixing your getter:你应该尝试修复你的吸气剂:

 /**
  * @return int
  */
 public function getType(): int
 {
    return $this->type;
 }

Also mind that you might have a problem in your choice array:还要注意,您的选择数组可能有问题:

// be careful: those two have the same value
'alert_type' => 3, 
'lightbulb_type' => 3,

This would surely cause an issue to Symfony, especially if it uses array_values in order to select the right choice out of the value of your entity.这肯定会给 Symfony 带来问题,特别是如果它使用array_values来从实体的值中选择正确的选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony ChoiceType 表单将选择映射到相关实体字段 - Symfony ChoiceType form map choices to a related entity field 嵌套表单/映射字段到实体 - Nested Form / Mapping field to entity Symfony 4 - 提交表单时,ChoiceType 字段为空 - Symfony 4 - ChoiceType field null when form submitted 根据 ChoiceType 上的选择动态加载 EntityType 字段 - Dynamically load EntityType field according to selection on ChoiceType Symfony 2,在实体上使用ChoiceType的createFormBuilder,choice_label是int - Symfony 2, createFormBuilder with ChoiceType on Entity, choice_label is int 如何在Doctrine中将外键既作为字段又作为映射映射到实体上? - How to map a foreign key on an entity both as field and mapping in Doctrine? Symfony:未选择任何选项时,Choicetype 字段不会在请求中返回 - Symfony: Choicetype Field not returning in Request when no option is selected 在Symfony中为我的ChoiceType表单字段添加“其他,请指定”选项 - Adding a 'other, please specify' option to my ChoiceType form field in Symfony Symfony 4 Forms:在 QueryBuilder 中为 ChoiceType 字段实现每个实体的自定义排序 - Symfony 4 Forms: implementing per-Entity custom sorting in QueryBuilder for ChoiceType fields Symfony3.0.3 ChoiceType通过复选框在实体条目中进行选择,并将选择的内容保存到simple_array MySQL - Symfony3.0.3 ChoiceType choose among entity entries via checkboxes and save the chosen to simple_array MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM