gpt4 book ai didi

symfony - 无法从序列化数据创建 DateTime 的实例

转载 作者:搜寻专家 更新时间:2023-10-30 22:47:59 24 4
gpt4 key购买 nike

我对日期有疑问。也许我在序列化过程中不明白某些东西,但是,当我向 Api-Platform (Symfony) 发送日期时间时,例如

dateEvaluation: "2017-12-05T11:52:00.000Z"

我收到这条错误信息

无法从序列化数据创建 DateTime 的实例,因为它的构造函数要求参数“time”存在。

这是我的实体

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
* @ORM\Entity(repositoryClass="App\Repository\EvaluationRepository")
* @ORM\HasLifecycleCallbacks()
* @ApiResource(attributes={
* "normalization_context"={
* "groups"={"Evaluation"}
* }
* })
*/
class Evaluation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"NotesEvaluations", "Evaluation"})
*/
private $id;

/**
* @var \DateTime
* @ORM\Column(type="datetime")
* @Groups({"Evaluation"})
*/
private $created;

/**
* @var \DateTime
* @ORM\Column(type="datetime")
* @Groups({"Evaluation"})
*/
private $updated;

/**
* @var \DateTime
* @ORM\Column(type="datetime")
* @Groups({"Evaluation"})
*/
private $dateEvaluation;

/**
* @var string
*
* @ORM\Column(type="text")
* @Groups({"Evaluation"})
*/
private $commentaire;

/**
* @var
*
* @ORM\ManyToOne(targetEntity="App\Entity\Personnel")
* @Groups({"NotesEvaluations", "Evaluation"})
*/
private $auteur;

/**
* @var
*
* @ORM\ManyToMany(targetEntity="App\Entity\Personnel")
*/
private $autorises;

/**
* @var integer
*
* @ORM\Column(type="integer")
*/
private $annee;

/**
* @var
*
* @ORM\ManyToOne(targetEntity="App\Entity\Module", inversedBy="evaluations")
* @Groups({"Evaluation"})
*/
private $module;

/**
* @var Boolean
*
* @ORM\Column(type="boolean")
* @Groups({"NotesEvaluations"})
*/
private $visible;

/**
* @var Boolean
*
* @ORM\Column(type="boolean")
* @Groups({"NotesEvaluations"})
*/
private $modifiable;

/**
* @var
*
* @ORM\ManyToOne(targetEntity="App\Entity\TypeGroupe")
* @Groups({"Evaluation"})
*/
private $typegroupe;

/**
* @var float
*
* @ORM\Column(type="decimal")
* @Groups({"Evaluation"})
*/
private $coefficient;

/**
* @return float
*/
public function getCoefficient(): float
{
return $this->coefficient;
}

/**
* @return mixed
*/
public function getTypegroupe()
{
return $this->typegroupe;
}

/**
* @param mixed $typegroupe
*/
public function setTypegroupe($typegroupe): void
{
$this->typegroupe = $typegroupe;
}

/**
* @return \DateTime
*/
public function getDateEvaluation(): \DateTime
{
return $this->dateEvaluation;
}

/**
* @param \DateTime $dateEvaluation
*/
public function setDateEvaluation(\DateTime $dateEvaluation): void
{
$this->dateEvaluation = $dateEvaluation;
}

/**
* @return string
*/
public function getCommentaire(): string
{
return $this->commentaire;
}

/**
* @param string $commentaire
*/
public function setCommentaire(string $commentaire): void
{
$this->commentaire = $commentaire;
}

/**
* @param float $coefficient
*/
public function setCoefficient(float $coefficient): void
{
$this->coefficient = $coefficient;
}

/**
* @return mixed
*/
public function getId()
{
return $this->id;
}

/**
* @return \DateTime
*/
public function getCreated(): \DateTime
{
return $this->created;
}

/**
* @param \DateTime $created
*/
public function setCreated(\DateTime $created): void
{
$this->created = $created;
}

/**
* @return \DateTime
*/
public function getUpdated(): \DateTime
{
return $this->updated;
}

/**
* @param \DateTime $updated
*/
public function setUpdated(\DateTime $updated): void
{
$this->updated = $updated;
}

/**
* @return mixed
*/
public function getAuteur()
{
return $this->auteur;
}

/**
* @param mixed $auteur
*/
public function setAuteur($auteur): void
{
$this->auteur = $auteur;
}

/**
* @return mixed
*/
public function getAutorises()
{
return $this->autorises;
}

/**
* @param mixed $autorises
*/
public function setAutorises($autorises): void
{
$this->autorises = $autorises;
}

/**
* @return int
*/
public function getAnnee(): int
{
return $this->annee;
}

/**
* @param int $annee
*/
public function setAnnee(int $annee): void
{
$this->annee = $annee;
}

/**
* @return mixed
*/
public function getModule()
{
return $this->module;
}

/**
* @param mixed $module
*/
public function setModule($module): void
{
$this->module = $module;
}

/**
* @return bool
*/
public function isVisible(): bool
{
return $this->visible;
}

/**
* @param bool $visible
*/
public function setVisible(bool $visible): void
{
$this->visible = $visible;
}

/**
* @return bool
*/
public function isModifiable(): bool
{
return $this->modifiable;
}

/**
* @param bool $modifiable
*/
public function setModifiable(bool $modifiable): void
{
$this->modifiable = $modifiable;
}

/**
* @ORM\PreUpdate
*/
public function updateDate(): void
{
$this->setUpdated(new \Datetime());
}

public function __construct()
{
$this->setCreated(new \Datetime());
$this->setUpdated(new \Datetime());

}
}

我不明白为什么缺少“时间”。也许它用于创建或更新的字段?无论如何,创建和更新的字段使用 ApiPlatform 返回一个带有时区的对象......

created:
{timezone: {name: "Europe/Paris",…}, offset: 561, timestamp: -62169984561}
offset:561
timestamp:-62169984561
timezone:{name: "Europe/Paris",…}

感谢您的帮助。大卫

最佳答案

我找到了一种方法来管理这个对我有用的错误。我在我的 config.yml 中这样声明 DateTimeNormalizer:

datetime_normalizer:
class: Symfony\Component\Serializer\Normalizer\DateTimeNormalizer
public: false
tags: [serializer.normalizer]

希望对您有所帮助!

关于symfony - 无法从序列化数据创建 DateTime 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48081265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com