5

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

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

我得到的错误是:

渲染模板期间抛出异常(“可捕获的致命错误:无法将类 DateTime 的对象转换为 /home/ikac/public_html/Symfony/app/cache/dev/twig/6f/eb/a068a5eed37d5c1eca1228cc7bb9 中的字符串.php 第 56 行") 在 DevAlbumBundle:Default: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>

感谢帮助!

4

1 回答 1

10

你能写你的树枝代码吗?

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

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

您可以在此处找到文档。

这个帖子有同样的问题。

于 2013-09-18T14:27:34.103 回答