我正在尝试向REST api发送包含两个时间戳的帖子请求。 问题是时间戳被标记为无效。 “这个值无效。”
我做错了什么?
这是请求:
POST http://localhost:8000/app_test.php/api/projects/1/tasks/1/timetrackings
Accept: application/json
Content-Type: application/json
{"timeStart":1390757625,"timeEnd":1390757625,"projectMember":1}
Controller如下所示:
class MemberController extends BaseApiController implements ClassResourceInterface
{
public function postAction($projectId, $taskId, Request $request)
{
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
$this->findProject($em, $projectId);
$task = $this->findTask($em, $projectId, $taskId);
$request->request->add(array(
'task' => $taskId,
));
$form = $this->createForm(new TimeTrackType(), new TimeTrack());
$form->submit($request->request->all());
if ($form->isValid())
{
/** @var TimeTrack $tracking */
$tracking = $form->getData();
$task->addTimeTrack($tracking);
$em->flush();
return $this->redirectView(
$this->generateUrl('api_get_project_task_timetracking', array(
'projectId' => $projectId,
'taskId' => $taskId,
'trackingId' => $tracking->getId(),
)),
Codes::HTTP_CREATED
);
}
return View::create($form, Codes::HTTP_BAD_REQUEST);
}
}
TimeTrackType类:
namespace PMTool\ApiBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class TimeTrackType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('timeStart', 'datetime', array(
'input' => 'timestamp',
))
->add('timeEnd', 'datetime', array(
'input' => 'timestamp',
))
->add('projectMember')
->add('task')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'PMTool\ApiBundle\Entity\TimeTrack',
'csrf_protection' => false,
));
}
/**
* @return string
*/
public function getName()
{
return 'timetrack';
}
}
实体类:
namespace PMTool\ApiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use \DateTime;
/**
* TimeTrack
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="PMTool\ApiBundle\Entity\TimeTrackRepository")
*/
class TimeTrack
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var DateTime
*
* @ORM\Column(name="timeStart", type="datetime")
*/
private $timeStart;
/**
* @var DateTime
*
* @ORM\Column(name="timeEnd", type="datetime")
*/
private $timeEnd;
/**
* @var ProjectMember
*
* @ORM\ManyToOne(targetEntity="ProjectMember")
*/
private $projectMember;
/**
* @var Task
*
* @ORM\ManyToOne(targetEntity="Task", inversedBy="timeTracks")
* @ORM\JoinColumn(name="taskId", referencedColumnName="id")
*/
private $task;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set timeStart
*
* @param DateTime $timeStart
* @return TimeTrack
*/
public function setTimeStart($timeStart)
{
$this->timeStart = $timeStart;
return $this;
}
/**
* Get timeStart
*
* @return DateTime
*/
public function getTimeStart()
{
return $this->timeStart;
}
/**
* Set timeEnd
*
* @param DateTime $timeEnd
* @return TimeTrack
*/
public function setTimeEnd($timeEnd)
{
$this->timeEnd = $timeEnd;
return $this;
}
/**
* Get timeEnd
*
* @return DateTime
*/
public function getTimeEnd()
{
return $this->timeEnd;
}
/**
* @return \PMTool\ApiBundle\Entity\Task
*/
public function getTask()
{
return $this->task;
}
/**
* @param \PMTool\ApiBundle\Entity\Task $task
* @return $this
*/
public function setTask($task)
{
$this->task = $task;
return $this;
}
/**
* @return \PMTool\ApiBundle\Entity\ProjectMember
*/
public function getProjectMember()
{
return $this->projectMember;
}
/**
* @param \PMTool\ApiBundle\Entity\ProjectMember $projectMember
* @return $this
*/
public function setProjectMember($projectMember)
{
$this->projectMember = $projectMember;
return $this;
}
}
答案 0 :(得分:0)
输入选项仅适用于基础数据对象的模型端。在你的情况下,它应该是日期时间。 您的问题是,您希望将时间戳转换为symfony表单日期时间表单类型识别的视图数据。不幸的是,我不知道该怎么做。
http://symfony.com/doc/current/reference/forms/types/datetime.html#input
答案 1 :(得分:0)
您可以使用变压器来实现这一目标。 (See Symfony Transformers)
以下是我的FormType示例:
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
...
$transformer = new DateTimeToTimestampTransformer();
$builder->add($builder->create('validFrom', 'text')
->addModelTransformer($transformer)
)
请务必使用"' text'"作为输入类型,否则它对我不起作用。