i'm stuck in error when try to generate CRUD in symfony2 I always get the following exception:
"Unable to transform value for property path "xxx": Expected a \DateTime or \DateTimeInterface."
it always happened with the any datetime field here is excerpt of my entity field:
/**
* @var \DateTime
*
* @ORM\Column(name="date_added", type="datetime", nullable=false)
*/
private $dateAdded = '0000-00-00 00:00:00';
/**
* Set dateAdded
*
* @param \DateTime $dateAdded
*
* @return User
*/
public function setDateAdded()
{
$this->dateAdded = new \DateTime();
return $this;
}
/**
* Get dateAdded
*
* @return \DateTime
*/
public function getDateAdded()
{
return $this->dateAdded;
}
-Also i tried to use easyadmin bundle which generate backend from entities using symfony2 CRUD on the fly but also get the same error so is there something wrong with my entity ?
The $dateAdded
field cannot contain a string. It needs to have a DateTime
object, because that's whats expected. In other words you need to have a constructor which sets the date:
/**
* @var \DateTime
*
* @ORM\Column(name="date_added", type="datetime", nullable=false)
*/
private $dateAdded;
public function __construct() {
$this->dateAdded = new \DateTime();
}
Also, you need to accept a parameter on your setDate
method:
public function setDate($date) {
$this->dateAdded = $date;
}
As a side note, keep in mind you will need to use a date filter if you'll be displaying the date in a twig template:
{{ entity.dateAdded|date('d.m.Y') }}
Maybe removing the annotation @param \DateTime $dateAdded
as your function setDateAdded()
has no parameter ?