2012-12-10 45 views
6

奥姆PHP/Symfony2的形式复选框字段

My\SampleBundle\Entity\Subject: 
    type: entity 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    fields: 

     // ... 

     motion: 
      type: smallint 
      unsigned: true 

类型

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    // ... 

    $builder->add('motion', 'checkbox', array(
     'required' => false 
    )); 

    // ... 
} 

错误

预期类型的​​变量 “布尔”, “整数” 给出


我想打开和关闭一个复选框。 该值由0和1分配。
即使赋值参数也没用。

$builder->add('motion', 'checkbox', array(
    'value'  => 1, 
    'required' => false 
)); 

我该怎么办?

回答

10

在您的ORM映射定义中,您必须将motion定义为布尔型而不是smallint。此外,Symfony将TINYINT解释为布尔值,并将其他整数SQL类型解释为整数。

My\SampleBundle\Entity\Subject: 
    type: entity 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    fields: 

     // ... 

     motion: 
      type: boolean 
+1

谢谢。你确实为我提供了方便。 –