2017-08-03 47 views
1

我希望用戶能夠手動輸入將被保存到數據庫的日期。我MonthType樣子:Symfony2 - 用DateType插入數據

class MonthType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
    $builder->add(
     'name', 
     TextType::class, 
     [ 
      'label' => 'label.name', 
      'required' => true, 
      'attr' => [ 
       'max_length' => 128, 
      ], 
     ] 
    ); 
    $builder->add(
     'dateStart', 
     DateType::class, 
     [ 
      'label' => 'label.dateStart', 
      'format' => 'yyyy-MM-dd', 
     ] 
    ); 
    $builder->add(
     'dateEnd', 
     DateType::class, 
     [ 
      'label' => 'label.dateEnd', 
      'format' => 'yyyy-MM-dd', 

     ] 
    ); 
    $builder->add(
     'upper_limit', 
     MoneyType::class, 
     [ 
      'label' => 'label.upper_limit', 
      'currency' => false, 
      'required' => true, 
     ] 
    ); 
    } 
} 

我的控制器看起來像:

class MonthController implements ControllerProviderInterface 
{ 

    public function connect(Application $app) 
    { 
     $controller->match('/add', [$this, 'addAction']) 
      ->method('POST|GET') 
      ->bind('month_add'); 

     return $controller; 
    } 
    /** 
    * Add action. 
    */ 
    public function addAction(Application $app, Request $request)        
    { 
     $month = []; 
     $userRepository = new UserRepository($app['db']); 

     $token = $app['security.token_storage']->getToken(); 
     if (null !== $token) { 
      $user = $token->getUser(); 
      $userLogin = $user->getUsername(); 
     } 

     $form = $app['form.factory']->createBuilder(MonthType::class, $month)->getForm(); 
     $form->handleRequest($request); 

     if ($form->isSubmitted() && $form->isValid()) { 
      $monthRepository = new MonthRepository($app['db']); 
      $monthRepository->save($form->getData(), $userLogin); 

      $app['session']->getFlashBag()->add(
       'messages', 
       [ 
        'type' => 'success', 
        'message' => 'message.element_successfully_added', 
       ] 
      ); 

      return $app->redirect($app['url_generator']->generate('month_index'), 301); 
     } 

     return $app['twig']->render(
      'history/add.html.twig', 
      [ 
       'month' => $month, 
       'form' => $form->createView(), 
       'user_id' => $userRepository->findUserId($userLogin), 
      ] 
     ); 
    } 
} 

而且在倉庫,我有:

/** 
* Save record. 
*/ 
public function save($month, $userLogin) 
{ 
    $user_id = $this -> findUserIdByLogin($userLogin); 
    $month['user_id'] = $user_id; 
    $month['remained'] = $month['upper_limit']; 

    if (isset($month['id']) && ctype_digit((string) $month['id']) && isset($user_id)) { 
     // update record 
     $id = $month['id']; 
     unset($month['id']); 

     return $this->db->update('month', $month, ['id' => $id]); 
    } else { 
     // add new record 
     return $this->db->insert('month', $month); 
    } 
} 

當我試圖保存記錄,我得到一個錯誤: 的使用params [「june 2012」,{「date」:「'INSERT INTO month(name,dateStart,dateEnd,upper_limit,user_id,remain)VALUES(?,?,?,?,?,?)' 2012-06-01 00:00:00.000000「,」t imezone_type「:3,」timezone「:」Europe/Berlin「},{」date「:」2012-06-30 00:00:00.000000「,」timezone_type「:3,」timezone「:」Europe/Berlin「} ,7600, 「1」,7600]:

捕致命錯誤:類的DateTime的對象無法轉換爲字符串

於是,我就做類似的東西:

public function save($month, $userLogin) 
    { 
     $user_id = $this -> findUserIdByLogin($userLogin); 
     $month['user_id'] = $user_id; 
     $month['remained'] = $month['upper_limit']; 

     $dateStart = new \DateTime($month['dateStart']); 
     $month['dateStart'] = $dateStart->format('Y-m-d'); 
     $dateEnd = new \DateTime($month['dateEnd']); 
     $month['dateEnd'] = $dateEnd->format('Y-m-d'); 
     ...// 
    } 

,但我收到: DateTime :: __ construct()期望參數1是字符串,對象給出

+0

whqt是你'$一個月[ 'dateStart']'?你甩了它嗎?它不是一個日期時間對象嗎? –

+0

dump($ month ['dateStart'])給出這個 DateTime {#678▼ +「date」:「2012-01-01 00:00:00.000000」 +「timezone_type」:3 +「timezone」: 「歐洲/柏林」 } – aniki2468

回答

1

似乎你有兩個$month['dateStart']$month['dateEnd']作爲DateTime(查看數據庫異常{"date":"2012-06-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"})。

這樣你就可以直接使用作爲$dateStart$dateEnd因爲它:

$month['dateStart']->format('Y-m-d')

+0

感謝您的回答。我試過這個,但是沒有我的修改,它給出了同樣的錯誤。 – aniki2468

+0

您是否嘗試過'New DateTime($ month ['dateStart'] - > format('Y-m-d')'? –

+0

是的,沒有變化。 – aniki2468

1

我解決了這個問題。我用這個:http://symfony.com/blog/new-in-symfony-3-1-datetime-normalizer 和寫

$serializer = new Serializer(array(new DateTimeNormalizer('Y-m-d'))); 
$month['dateStart'] = $serializer->normalize($month['dateStart']); 
$serializer = new Serializer(array(new DateTimeNormalizer('Y-m-d'))); 
$month['dateEnd'] = $serializer->normalize($month['dateEnd']);