我需要通过控制台发送Postmal或Curl字段DateTime for我的REST Api。 但是当我发送的值总是返回给我一个无效的表单,因此它不会插入到我的数据库中。 如果我尝试将该字段从日期时间更改为字符串,并与正常工作的字符串的请求,所以这个问题是sended我认为Symfony 3日期时间卷曲请求
值。这是我的实体生日
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Birthday
*
* @ORM\Table(name="birthday")
* @ORM\Entity(repositoryClass="AppBundle\Repository\BirthdayRepository")
*/
class Birthday
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\Column(name="birthday", type="datetime")
*/
private $birthday;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set birthday
*
* @param \DateTime $birthday
*
* @return Birthday
*/
public function setBirthday($birthday)
{
$this->birthday = $birthday;
return $this;
}
/**
* Get birthday
*
* @return \DateTime
*/
public function getBirthday()
{
return $this->birthday;
}
}
这是我的控制器内部REST API:
/**
* @Rest\Post("/users_birthday")
*/
public function postBirthdayAction(Request $request)
{
$birthday = new Birthday();
$form = $this->createForm(BirthdayType::class, $birthday);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($birthday);
$em->flush();
return $birthday;
//return new JsonResponse([$data, $status, $headers, $json])
}
throw new HttpException(400, "Invalid data");
}
我已经试过这样:
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"birthday" : "2010-10-10 10:30:11"
}
' "http://symfony-rest-api.app/users_birthday"
这:
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"birthday" : "04/21/15 20:56:16"
}
' "http://symfony-rest-api.app/users_birthday"
这:
curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{
"birthday" : "Tue, 02 Apr 2013 10:29:13 GMT"
}
' "http://symfony-rest-api.app/users_birthday"
没有一部作品。
有人可以帮助我吗?谢谢
谢谢!它的功能就像一个魅力! –