doutan1875 2016-03-03 18:35
浏览 51
已采纳

在EntityType-field中为1:N Doctrine关系选择null值

I got incidents which have a reference to a risk.

I am using Symfony 2.6 and Doctrine ORM entities with the following rather symbolic entities:

class Incident
{
    private $id;
    private $name;
    private $date;
    /**
     * @ORM\ManyToOne(targetEntity="Risk")
     * @ORM\JoinColumn(name="risk_id")
     */
    private $risk;
}

class Risk
{
    private $id;
    private $level;
    private $name;
}

The risks-Table has data like:

id  level   name
1   0       none
2   2       low
3   1       very low
4   5       moderate
5   10      high

For the incidents I got a form-type like this:

class IncidentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
[...]
            ->add('risk', 'entity', [
                'class' => 'Model:Risk',
                'property' => 'name',
                'empty_data' => null,
                'empty_value' => 'not assessed',
                'expanded' => true,
                'required' => [true|false](see below),
            ])
[...]
}

So my incidents risk can be 'not assessed'. That is when there is no risk set (=null). To keep my model clean I would not want to have a row with a level of -1 and a name of 'not assessed'.

My form works as expected, missing one tiny detail: when the risk is null, my list of radiobuttons has no value selected. This happens regardless of the value of the 'required'-option.

For a null-value I get:

( ) not assessed
( ) none
( ) very low
[...]

I would want:

(x) not assessed
( ) none
( ) very low
[...]

I guess that behaviour is just fine for a (not expanded) drop-down field.

Is there a way to have the 'placeholder'/'empty_value'-option of my radiobuttons selected when the data-value is null?

  • 写回答

2条回答 默认 最新

  • dongtidai6519 2016-03-04 08:36
    关注

    For your risk field you should do:

    class IncidentType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name')
            [...]
                ->add('risk', 'entity', [
                    'class' => 'Model:Risk',
                    'property' => 'name',
                    'empty_data' => null, 
                    'empty_value' => 'not assessed', 
                    'expanded' => true, 
                    'required' => true
                ])
        [...]
        }
    }
    

    I just checked that and it works, the first value is selected when the risk is empty and when using the above code. Note I added 'empty_data' => null, 'empty_value' => 'not assessed', and removed placeholder.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集