Symfony 3 DateTime from array conversion

Viewed 1362

In Symfony 3, I have the following object

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

/**
 * Shortcut
 *
 * @ORM\Table(name="shortcut", indexes={@ORM\Index(name="idx_user_id", columns={"user_id"}), @ORM\Index(name="idx_short", columns={"short"})})
 * @ORM\HasLifecycleCallbacks()
 * @ORM\Entity
 */
class Shortcut
    {
        /**
         * @var \DateTime
         *
         * @ORM\Column(name="creation", type="datetime", nullable=true)
         */
        private $creation;

         /**
         * Set creation
         *
         * @param \DateTime $creation
         *
         * @return Shortcut
         */
        public function setCreation($creation)
        {
            $this->creation = $creation;

            return $this;
        }

        /**
         * Get creation
         *
         * @return \DateTime
         */
        public function getCreation()
        {
            return $this->creation;
        }
    }

When I call getCreation() I have the date, so in twig I do

{{ shortcut.creation | localizeddate('medium', 'none', null) }}

but I receive

An exception has been thrown during the rendering of a template ("Notice: Array to string conversion")

Because the format is an array and not an instance of DateTime, I have tested with $shortcut->getCreation() instanceof \DateTime

So I have tried to do

public function getCreation()
{
    return new \DateTime($this->creation);
}

but it awaits for a string... Is it a problem with my database?

2 Answers
Related