【发布时间】:2019-07-18 14:30:56
【问题描述】:
我的问题与this question 非常相关,但并不完全相同。这是my previous one 上的后续问题,如果您已经阅读过它,但它并不那么依赖它,所以只阅读这个问题就足够了。我有这种方法可以在“小屋”上保存“触摸”(我知道这很奇怪)。我(尝试)这样做是从 POST 请求中提取信息,并使用 POSTMAN 对其进行测试。
/**
* @param Request $request
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \ErrorException
* @return JsonResponse
*/
public function registerTouch(Request $request)
{
$touchService = new TouchService($this->entityManager);
$cabinet = $request->get('cabinet_id');
/**
* @var $touch Touch
*/
$touch = new Touch(
$request->get('time'),
$request->get('toucher'),
$request->get('cabinet_id'),
$request->get('id')
);
if (empty($cabinet)) {
return new JsonResponse(['error' => 'Touch not saved'], 200);
} else {
$touch->setCabinet($cabinet);
$touchService->registerTouch($touch);
return new JsonResponse(['success' => 'Touch saved'], 200);
}
Touch 类包含以下内容:
<?php
namespace App\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\TouchRepository")
*/
class Touch implements \JsonSerializable
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime")
*/
private $time;
/**
* @ORM\Column(type="string", length=255)
*/
private $toucher;
private $accountId;
/**
* @ORM\ManyToOne(targetEntity="Cabinet")
*/
private $cabinet;
/**
* Touch constructor.
* @param DateTime $time
* @param string $toucher
* @param Cabinet $cabinet
* @param int $id
*/
public function __construct(DateTime $time, string $toucher, Cabinet $cabinet = null, int $id = null)
{
$this->time = $time;
$this->toucher = $toucher;
$this->cabinet = $cabinet;
$this->id = $id;
}
public function getId(): int
{
return $this->id;
}
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
public function getTime(): DateTime
{
return $this->time;
}
public function getToucher(): string
{
return $this->toucher;
}
public function getCabinet(): Cabinet
{
return $this->cabinet;
}
public function setCabinet(Cabinet $cabinet): self
{
$this->cabinet = $cabinet;
$this->accountId = $cabinet->getId();
return $this;
}
public function getAccountId(): int
{
return $this->accountId;
}
public function setAccountId(int $accountId): self
{
$this->accountId = $accountId;
return $this;
}
public function jsonSerialize()
{
return get_object_vars($this);
}
}
但是在运行此代码时出现此错误:
传递给 App\Entity\Touch::__construct() 的参数 1 必须是 DateTime 的实例,给定 null,在第 89 行的 /var/www/learningProject/src/Controller/APITouchController.php 中调用(500 内部服务器错误)
我正在使用 POSTMAN 传递数据:
[
{
"id": 666,
"cabinet_id": 55,
"time": {
"date": "2018-06-18 11:51:22.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"toucher": "person1",
}
]
哪个是 DateTime 对象的正确布局,所以我不确定为什么会发生这个错误,也不知道如何解决它。任何帮助将不胜感激!
【问题讨论】:
-
拥有正确的格式并不能使其成为
DateTime对象,您必须在某个时候对其进行转换。见this question。 -
@ehymel 这不会导致我丢失格式和可能的时区等数据吗?这将如何工作?因为我需要将“触摸”存储在数据库中。
-
@ehymel 我也用这个
"time": "2018-06-18 11:51:22",尝试了上面的time,但我仍然得到同样的错误。 -
您的
Touch::__construct()函数需要一个 phpDateTime对象。但是当您调用$touch = new Touch(...)时,您实际上是在传递一个字符串,无论该字符串是否采用某种特定格式。在调用new Touch(...)之前,您必须满足您自己代码的类型要求。请参阅我的第一条评论。 -
试试
dump($request),告诉我们你有什么。