Reputation: 33
I have a ORM\Entity with a column date type. When I use EntityManager to select object to Update I have these error: "Catchable Fatal Error: Object of class DateTime could not be converted to string"
Column:
/**
* @ORM\Id
* @ORM\Column(name="date", type="date")
*/
private $date;
And the query:
$query = $em->createQuery('SELECT t FROM AppBundle:Table t WHERE t.id = :t_id')->setParameter('t_id', $tId);
$result = $query->getResult(); // <- ¡ERROR IS HERE!
ERROR:
Catchable Fatal Error: Object of class DateTime could not be converted to string
Thanks a lot!
Upvotes: 3
Views: 1292
Reputation: 1
This is one of the reason why it is not advised at all to use a DateTime as a unique id.
Another way to do it is use set your primary key of the String type, and do:
$entity->setId(new \DateTime()->format('yyyy/mm/dd'));
See https://stackoverflow.com/a/18874646/3062315 for reference!
Upvotes: 0
Reputation: 1497
A date/datetime can't be used as a unique ID with Doctrine, because it requires a __toString() method. You can create this class :
class DoctrineDateTime extends DateTime
{
public function __toString()
{
return $this->format('U');
}
}
Instead of setting your "$date" field with a DateTime object, use this class. Do the same thing for your requests on the ID.
Upvotes: 3