Symfony2 DateTime无法转换为字符串

时间:2013-09-18 14:02:02

标签: php symfony

大家好,我在创建和修改某些数据时尝试渲染日期时遇到问题。

我有专辑包,当我创建新的专辑项目时,我在数据库日期插入该专辑创建时和修改时间。我成功将数据插入数据库,但只有当我尝试渲染时才会出现问题。

我得到的错误是:

  

在渲染模板期间抛出了异常   (“Catchable Fatal Error:类DateTime的对象不可能   转换为字符串   /home/ikac/public_html/Symfony/app/cache/dev/twig/6f/eb/a068a5eed37d5c1eca1228cc7bb9.php   DevAlbumBundle中的第56行“:默认值:index.html.twig第19行.500   内部服务器错误 - Twig_Error_Runtime 1链接异常:

ContextErrorException »

检查我的实体和控制器是否有此操作:

<?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;
    }
}

我也试试这个:

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

    return $this;
}

控制器操作:

/**
 * 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()); 
}

更新:

<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>

感谢您的帮助!

1 个答案:

答案 0 :(得分:8)

你能写出你的枝条代码吗?

我认为您必须使用日期过滤器,如下所示:

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

您可以找到文档here

This post也有同样的问题。