2016-05-03 31 views
1

我搜索了很多,找不到任何在StackOverflow的我的问题......问题与Symfony的插入DateType场3种形式

我有这样的结构时:(续)

$form = $this->createFormBuilder($vendedor) 
      // ... 
      ->add('datanascimento', DateType::class, [ 
       'label' =>'D.Nascimento', 
       'attr' =>['class'=>'input-sm'], 
       'format'=>'d-M-yyyy', 
       'years'=>range(1970, 2010) 
      ]) 
      // ... 
      ->getForm(); 

而且我在我的实体此CONFIGS:

//... 

/** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="dataNascimento", type="date", nullable=true) 
    */ 
    private $datanascimento; 

//... 

/** 
    * Set datanascimento 
    * 
    * @param string $datanascimento 
    * 
    * @return CadPfPj 
    */ 
    public function setDatanascimento($datanascimento) 
    { 
     $this->datanascimento = $datanascimento; 

     return $this; 
    } 

    /** 
    * Get datanascimento 
    * 
    * @return string 
    */ 
    public function getDatanascimento() 
    { 
     return $this->datanascimento; 
    } 

当我尝试插入一个新的寄存器,我得到这个错误:

Catchable Fatal Error: Object of class DateTime could not be converted to string 
500 Internal Server Error - ContextErrorException 

当我调试,并转储对象......我发现这一点:

//... 
-datanascimento: DateTime {#530 ▼ 
    +"date": "1970-01-01 00:00:00.000000" 
    +"timezone_type": 3 
    +"timezone": "America/Sao_Paulo" 
    } 
//... 

我的数据库是MySQL的和字段类型为datetime ... 如何配置的symfony停止发送阵列并只发送“日期”?

感谢您的帮助!

回答

0

您需要更改widgettype of form entry - 默认为choice,但你需要textsingle_text

 ->add('datanascimento', DateType::class, [ 
      'label' =>'D.Nascimento', 
      'widget' =>'single_text',   
      'attr' =>['class'=>'input-sm'], 
      'format' =>'d-M-yyyy', 
      'years' =>range(1970, 2010) 
     ])