Php 无法将Symfony2 DateTime转换为字符串

Php 无法将Symfony2 DateTime转换为字符串,php,symfony,Php,Symfony,大家好,我有问题,当我试图渲染日期时,是一些数据创建和修改 我有相册包,当我创建新相册项目时,我会在数据库中插入该相册的创建日期和修改日期。我成功地将该数据插入数据库,但只有在尝试渲染时才出现问题 我得到的错误是: /** * Album Add * * @Route("/album/add") * * @return @return Dev\AlbumBundle\Controller\DefaultController:add() */ public function ad

大家好,我有问题,当我试图渲染日期时,是一些数据创建和修改

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

我得到的错误是:

/**
 * 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()); 
}
在呈现模板期间引发了异常 (“可捕获的致命错误:无法捕获类DateTime的对象 在中转换为字符串 /home/ikac/public_html/Symfony/app/cache/dev/twig/6f/eb/a068a5eed37d5c1eca1228cc7bb9.php DevalBumble中的第56行“:默认值:第19行的index.html.twig。500 内部服务器错误-细枝错误\u运行时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;
    }
}
控制器操作:

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

#身份证
标题
描述
背景
创建日期
修改日期
行动
{相册%中项目的%s}
{{item.id}
{{item.title}
{{item.description}}
{{item.background}}
{{item.dateCreated}date('Y-m-dh:i:s')}
{{item.dateModified}date('Y-m-dh:i:s')}
{%endfor%}

谢谢你的帮助

你能写你的小树枝代码吗

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

{{ album.dateCreated|date("m/d/Y") }}
您可以找到文档


也有同样的问题。

我在twig中设置了日期过滤器,您可以看到。好的,我没有看到您的更新,您是否尝试清除缓存?我认为这不会解决你的问题,但这是我能告诉你的最后一件事(对不起,可能是重复的