Alex Rivera | Logout

Symfony/Doctrine: DateTime as primary key

Asked 2013-06-15T16:50:53.463
8

I am trying to make an Entity using a date as a primary key. The problem is that Symfony can't convert the DateTime I'm using into a string to introduce it in the IdentityMap. I get the following error during the persist of the entity:

Catchable Fatal Error: Object of class DateTime could not be converted to string in..

I'm using this code in the entity:

/**
 * @ORM\Id
 * @ORM\Column(type="datetime")
 */
protected $date;

The error appears in the entity repository:

$em = $this->getEntityManager();
$currentData = new CurrentData();
...
$currentData->setDate(new \DateTime($dateStr));
...
$em->persist($currentData);
$em->flush();

How can I solve this problem? Thank you.

Edit
Report

1 Answer

0

You probably should just use a normal sequence.

However, if you must use calendar information to key, you might want to store it as a 'string' type and then use php's DateTime Format:

 $currentData->setDate(new \DateTime($dateStr)->format('yyyy/mm/dd');

You are probably headed for misery if you try to use a datetime object for key.

answered 2013-06-15T17:17:23.857

Your Answer