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:
在呈现模板时抛出了一个异常(“可捕获的致命错误:类DateTime的对象无法在/home/ikac/public_html/Symfony/app/cache/dev/twig/6f/eb/a068a5eed37d5c1eca1228cc7bb9中转换为string)。在DevAlbumBundle中的)默认值:index.html。在19行树枝。内部服务器错误- Twig_Error_Runtime 1链接异常:
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!
谢谢你的帮助!