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; class Albums { private $id; private $title; private $description; private $background; private $dateCreated; private $dateModified; public function getId() { return $this->id; } public function setTitle($title) { $this->title = $title; return $this; } public function getTitle() { return $this->title; } public function setDescription($description) { $this->description = $description; return $this; } public function getDescription() { return $this->description; } public function setBackground($background) { $this->background = $background; return $this; } public function getBackground() { return $this->background; } public function setDateCreated() { $this->dateCreated = new \DateTime(); return $this; } public function getDateCreated() { return $this->dateCreated; } public function setDateModified() { $this->dateModified = new \DateTime(); return $this; } public function getDateModified() { return $this->dateModified; } }
I also try:
public function setDateCreated() { $this->dateCreated = new \DateTime(date('Ymd H:i:s')); return $this; }
Controller action:
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!
Ivan source share