Symfony2 DateTime cannot be converted to string

Hi guys, I have a problem when I try to display the date when some data is created and modified.

I have an album package, and when I create a new album item, I insert in the database the date when this album is created and when it is changed. I successfully insert this data into the database, but I only have a problem when trying to render.

The error I am getting is:

An exception was thrown during template rendering ("Catchable Fatal Error: A DateTime class object cannot be converted to a string in / home / ikac / public _html / Symfony / app / cache / dev / twig / 6f / eb / a068a5eed37d5c1eca1228cc7bb9.php line 56 ") in DevAlbumBundle: default: index.html.twig on line 19. 500 Internal server error - Twig_Error_Runtime 1 associated Exception:

ContextErrorException ยป 

Check my objects and controller for the following:

 <?php namespace Dev\AlbumBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Albums * * @ORM\Table(name="albums") * @ORM\HasLifecycleCallbacks() * @ORM\Entity(repositoryClass="Dev\AlbumBundle\Entity\AlbumsRepository") */ class Albums { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="title", type="string", length=255) */ private $title; /** * @var string * * @ORM\Column(name="description", type="text") */ private $description; /** * @var string * * @ORM\Column(name="background", type="string", length=255) */ private $background; /** * @var \DateTime * * @ORM\Column(name="date_created", type="datetime", nullable=true) */ private $dateCreated; /** * @var \DateTime * * @ORM\Column(name="date_modified", type="datetime", nullable=true) */ private $dateModified; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set title * * @param string $title * @return Albums */ public function setTitle($title) { $this->title = $title; return $this; } /** * Get title * * @return string */ public function getTitle() { return $this->title; } /** * Set description * * @param string $description * @return Albums */ public function setDescription($description) { $this->description = $description; return $this; } /** * Get description * * @return string */ public function getDescription() { return $this->description; } /** * Set background * * @param string $background * @return Albums */ public function setBackground($background) { $this->background = $background; return $this; } /** * Get background * * @return string */ public function getBackground() { return $this->background; } /** * Set dateCreated * * @ORM\PrePersist * @return Albums */ public function setDateCreated() { $this->dateCreated = new \DateTime(); return $this; } /** * Get dateCreated * * @return \DateTime */ public function getDateCreated() { return $this->dateCreated; } /** * Set dateModified * * @ORM\PreUpdate * @return Albums */ public function setDateModified() { $this->dateModified = new \DateTime(); return $this; } /** * Get dateModified * * @return \DateTime */ public function getDateModified() { return $this->dateModified; } } 

I also try:

 /** * Set dateCreated * * @ORM\PrePersist * @return Albums */ public function setDateCreated() { $this->dateCreated = new \DateTime(date('Ymd H:i:s')); return $this; } 

Controller action:

 /** * Album Add * * @Route("/album/add") * * @return @return Dev\AlbumBundle\Controller\DefaultController:add() */ public function addAction() { $album = new Albums(); $album->setTitle("Nojeva Barka"); $album->setDescription("Album Bore Corbe"); $album->setBackground("background.png"); $em = $this->getDoctrine()->getManager(); $em->persist($album); $em->flush(); return new Response('Album created '. $album->getTitle()); } 

UPDATE:

 <table border="1"> <thead> <th> #ID </th> <th> Title </th> <th> Description </th> <th> Background </th> <th> Date Created </th> <th> Date Modified </th> <th> Action </th> </thead> <tbody> {% for item in album %} <tr> <td> {{ item.id }} </td> <td> {{ item.title }} </td> <td> {{ item.description }} </td> <td> {{ item.background }} </td> <td> {{ item.dateCreated|date('Ymd H:i:s') }} </td> <td> {{ item.dateModified|date('Ymd H:i:s') }} </td> <td> <a href="edit"> Edit </a> <a href="delete"> Delete </a> </td> </tr> {% endfor %} </tbody> </table> 

Thanks for the help!

+4
source share
1 answer

Can you write your twig code?

I think you need to use a date filter, for example:

 {{ album.dateCreated|date("m/d/Y") }} 

You can find the documentation here .

This post has the same issue.

+8
source

Source: https://habr.com/ru/post/1502825/


All Articles