I need to deserialize a JSON file (which is a table from an old database) into a "Training" entity object.
The problem I am encountering is an error :
Failed to denormalize attribute "start_at" value for class "App\Entity\Training": Expected argument of type "DateTimeImmutable", "string" given at property path "start_at".
The serializer is called in the construct: (What is commented is a test without using the SerializerInterface)
public function __construct(
string $projectDir,
SerializerInterface $serializer,
ManagerRegistry $doctrine
) {
$this->projectDir = $projectDir;
$this->serializer = $serializer;
$this->doctrine = $doctrine;
// $encoders = [new JsonEncoder()];
// $normalizers = [new ObjectNormalizer(null, null, null, new ReflectionExtractor()), new DateTimeNormalizer()];
// $this->serializer = new Serializer($normalizers, $encoders);
}
JSON file:
{
"id": "13",
"type_id": "3",
"title": "Technical training",
"description": "the description",
"start_at": "2018-05-14 09:00:00",
"end_at": "2018-05-18 17:00:00",
"pj": "file.pdf",
"places_dispos": "2",
"created_at": "2018-01-15 10:20:59",
"updated_at": "2019-04-29 18:01:55"
}
My function:
public function readJsonFile(string $fileName, string $tableName)
{
$file = $this->projectDir . '/var/json-database/extranet_table_' . $fileName . '.json';
$fileString = file_get_contents($file);
$fileObj = json_decode($fileString, true);
return $this->convertJsonToObject($fileObj['data'], $tableName);
}
private function convertJsonToObject(array $arrayData, string $tableName)
{
$arrayTableObj = [];
foreach ($arrayData as $data) {
$data['id'] = intval($data['id']);
//Boolean
if (isset($data['active'])) {
($data['active'] === '1') ? $data['active'] = true : $data['active'] = false;
}
if (isset($data['updatable'])) {
($data['updatable'] === '1') ? $data['updatable'] = true : $data['updatable'] = false;
}
$dataJson = json_encode($data);
// $dataObj = $this->serializer->deserialize($dataJson, "App\Entity\\". $tableName, 'json');
$dataObj = $this->serializer->deserialize($dataJson, "App\Entity\\". $tableName, 'json', [
DateTimeNormalizer::FORMAT_KEY => 'Y-m-d H:i:s',
]);
$dataObj->setId($data['id']);
$arrayTableObj[] = $dataObj;
}
return $this->importInDatabase($arrayTableObj);
}
In my Training entity:
#[ORM\Column(type: 'datetime_immutable')]
private $startAt;
#[ORM\Column(type: 'datetime_immutable')]
private $endAt;
I tried to transform the content of "start_at" into DateTimeImmutable or DateTime, it doesn't change anything except the error message (array given).
I think I understood my mistake. The displayed error did not help me. The problem I think, is that the property "start_at" in base is different from my entity "startAt".
If I rename "start_at" in the JSON to "startAt" it works. I don't delete the question, if it can help people not to make the same mistake as me.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.