Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 DateTime could not be converted to string

Tags:

php

symfony

Hello guys i have problem when i try to render date when is some data created and modified.

I have Album bundle and when i create new Album item i insert in database date when is that album created and when is modified. I success insert that data in database but i have problem only when i try to render.

Error what i get is :

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class DateTime could not be converted to string in /home/ikac/public_html/Symfony/app/cache/dev/twig/6f/eb/a068a5eed37d5c1eca1228cc7bb9.php line 56") in DevAlbumBundle:Default:index.html.twig at line 19. 500 Internal Server Error - Twig_Error_Runtime 1 linked Exception:

ContextErrorException »

Check my Entity and controller for this actions:

<?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 this :

/**
 * Set dateCreated
 *
 * @ORM\PrePersist
 * @return Albums
 */
public function setDateCreated()
{
    $this->dateCreated = new \DateTime(date('Y-m-d 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('Y-m-d H:i:s') }} </td>
                    <td> {{ item.dateModified|date('Y-m-d H:i:s') }} </td>
                    <td> <a href="edit"> Edit </a> <a href="delete"> Delete </a> </td>
                </tr>
            {% endfor %}
        </tbody>
</table>

Thanks for help!

like image 303
Ivan Avatar asked Sep 18 '13 14:09

Ivan


1 Answers

Can you write your twig code?

I think you have to use the date filter, like this:

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

You can find the documentation here.

This post has the same problem.

like image 58
NewRehtse Avatar answered Sep 25 '22 01:09

NewRehtse