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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀