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 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?