在整数日期时调用成员函数format()

时间:2017-08-03 08:02:34

标签: rest api symfony postman

我正在尝试通过POSTMAN通过REST API将条目发布到db。我正在使用symfony框架。

{
    "error": {
        "code": 500,
        "message": "Internal Server Error",
        "exception": [
            {
                "message": "Error: Call to a member function format() on integer",
                "class": "Symfony\\Component\\Debug\\Exception\\FatalErrorException",
                "trace": [
                    {
                        "namespace": "",
                        "short_class": "",
                        "class": "",
                        "type": "",
                        "function": "",
                        "file": "C:\\xampp\\htdocs\\joel\\proj3\\vendor\\doctrine\\dbal\\lib\\Doctrine\\DBAL\\Types\\DateTimeType.php",
                        "line": 53,
                        "args": []
                    }
                ]
            }
        ]
    }
}

这是代码:

<?php

namespace AcsUserBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;
use AcsUserBundle\Entity\User;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

class UserController extends FOSRestController
{


   /**
 * @Rest\Post("/user/")
 */
 public function postAction(Request $request)
 {
   $data = new User;
   $personid = $request->get('personid');
   $privilagevalueid = $request->get('privilagevalueid');
   $username = $request->get('username');
   $password = $request->get('password');



   $createdate = strtotime($request->get('createdate'));



 if(empty($personid) || empty($privilagevalueid) || empty($username)|| empty($password)|| empty($createdate))
 {
   return new View("NULL VALUES ARE NOT ALLOWED", Response::HTTP_NOT_ACCEPTABLE); 
 } 



  $data->setPersonid($personid);
  $data->setPrivilagevalueid($privilagevalueid);
  $data->setUsername($username);
  $data->setPassword($password);
  $data->setCreatedate($createdate);



  $em = $this->getDoctrine()->getManager();
  $em->persist($data);
  $em->flush();
  return new View("User Added Successfully", Response::HTTP_OK);
 }


}

3 个答案:

答案 0 :(得分:3)

我猜你的setCreatedate setter正在等待DateTime对象。

您可以像这样更改$createdate变量;

$createdate = new \DateTime($request->get('createdate'));

答案 1 :(得分:1)

除非您的日期格式类似于Y-m-d,否则您将收到此解决方案的错误。

$createdate = new \DateTime($request->get('createdate'));

您应该知道日期格式,并且可以使用以下选项。

$createDate = $request->get('createdate');

$createDateObj = \DateTime::createFromFormat('d/m/Y', $createDate);
$createDateObj = \DateTime::createFromFormat('d.m.Y', $createDate);
$createDateObj = \DateTime::createFromFormat('m/d/Y', $createDate);

答案 2 :(得分:0)

我不建议您手动从请求中获取数据并创建实体。为此,您可以使用表格,您还将获得奖励验证:)

public function postTeamAction(Request $request)
{
    $postData = json_decode($request->getContent(), true);
    $newTeam = new Team();
    $form = $this->createForm(TeamType::class, $newTeam);
    $form->submit($postData);
    if ($form->isValid()) {
        $this
            ->getDoctrine()->getManager()
            ->persist($newTeam)
        ;
        $this
            ->->getDoctrine()->getManager()
            ->flush()
        ;

    }

在表单中,您可以定义每个字段应该是什么类型。