Symfony 4,传递给..的参数1必须是DateTime的实例,给定null

时间:2019-07-18 14:30:56

标签: symfony datetime symfony4

我的问题与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对象的正确布局,所以我不确定为什么会发生此错误,也不确定如何解决它。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

问题是您要向Touch的构造函数传递一个字符串,但是要用DateTime $time进行类型提示,因此它需要一个DateTime对象。

要解决您的问题,请先将字符串转换为DateTime,然后再将其传递给构造函数。

/** @var $cabinet Cabinet|null */
$cabinet = $this->entityManager->getRepository(Cabinet::class)->findOneBy([
    'id' => $request->get('cabinet_id')
]);

if (null === $cabinet) {
    return new JsonResponse(['error' => 'Touch not saved'], 200);
}

/** @var $touch Touch */
$touch = new Touch(
    new \DateTime($request->get('time')),
    $request->get('toucher'),
    $cabinet,
    (int)$request->get('id')
);
$touchService->registerTouch($touch);

return new JsonResponse(['success' => 'Touch saved'], 200);

提示:考虑使用DateTimeImmutable代替DateTime