我无法使用 Symfony 定义我的日期时间参数。
如果 last_scan_date 为空,我试图返回空,如果不返回结果!
错误说:
传递给 checkLastScan() 的参数 2 必须是 DateTime 的实例,给出的字符串
我的功能:
public function checkLastScan($hash, \DateTime $lastScanDate)
{
$findLastScan = $this->getMyRepository()->findOneBy([
'hash' => $hash,
'lastScanDate' => $lastScanDate
]);
if (!$findLastScan) {
throw new \Exception('Not found!');
}
if ($lastScanDate === null) {
return null;
} else {
return $findLastScan;
}
}
和我的电话:
$this->requirePostParams(['hash', 'last_scan_date']);
$this->container->get('app')->checkLastScan(
$this->data['hash'],
$this->data['last_scan_date']
);
return $this->success();
和实体:
/**
* @ORM\Column(name="last_scan_date", type="date", nullable=true)
*/
private $lastScanDate;
也许问题是在需要 post params 时,例如:
/**
* Require post params
*
* @param $params
*/
protected function requirePostParams($params)
{
$currentRequest = $this->get('request_stack')->getCurrentRequest();
$postData = $currentRequest->request->all();
$postContent = json_decode($currentRequest->getContent(), true);
if (!empty($postContent)) {
$postData = $postContent;
}
$this->data = $postData;
$missingParams = [];
foreach ($params as $param) {
if (!array_key_exists($param, $postData)) {
$missingParams[] = $param;
}
}
蛊毒传说