DDC-3377: DateTime columns cannot be used with @Id #4174

Closed
opened 2026-01-22 14:36:36 +01:00 by admin · 13 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 6, 2014).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user cverges-ch:

DateTimeIdTest.php:

use Doctrine\ORM\Mapping as ORM;

/****
 * @ORM\Table(name="datetime*id*test")
 * @ORM\Entity
 */
class DateTimeIdTest
{
    /****
     * @ORM\Column(name="when", type="datetime")
     * @ORM\Id
     */
    public $when;

    public function **construct()
    {
        $this->when = new \DateTime();
    }
}

test.php:

require_once('DateTimeIdTest.php');

$entity = new DateTimeIdTest();
$entityManager->persist($entity);

Output:

Object of class DateTime could not be converted to string

/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1359
/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1172
/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:864
/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1635
/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1591
/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:624
/app/cache/test/jms*diextra/doctrine/EntityManager*545bc3b10fd93.php:94
test.php:4

I was able to debug this down to where the implode() call in Doctrine\ORM\UnitOfWork::addToIdentityMap() is what is triggering this problem. It attempts to convert all contained entities in the Doctrine\ORM\UnitOfWork::entityIdentifiers array to strings. For a DateTime object, the DateTime::format() function should be used instead of relying on **toString().

Originally created by @doctrinebot on GitHub (Nov 6, 2014). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user cverges-ch: **DateTimeIdTest.php:** ``` use Doctrine\ORM\Mapping as ORM; /**** * @ORM\Table(name="datetime*id*test") * @ORM\Entity */ class DateTimeIdTest { /**** * @ORM\Column(name="when", type="datetime") * @ORM\Id */ public $when; public function **construct() { $this->when = new \DateTime(); } } ``` **test.php:** ``` require_once('DateTimeIdTest.php'); $entity = new DateTimeIdTest(); $entityManager->persist($entity); ``` **Output:** ``` Object of class DateTime could not be converted to string /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1359 /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1172 /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:864 /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1635 /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1591 /vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:624 /app/cache/test/jms*diextra/doctrine/EntityManager*545bc3b10fd93.php:94 test.php:4 ``` I was able to debug this down to where the implode() call in Doctrine\ORM\UnitOfWork::addToIdentityMap() is what is triggering this problem. It attempts to convert all contained entities in the Doctrine\ORM\UnitOfWork::entityIdentifiers array to strings. For a DateTime object, the DateTime::format() function should be used instead of relying on **toString().
admin added the BugDuplicate labels 2026-01-22 14:36:36 +01:00
admin closed this issue 2026-01-22 14:36:36 +01:00
Author
Owner
@doctrinebot commented on GitHub (Nov 6, 2014): - duplicates [DDC-2768: Doctrine could not work with date as primary key](http://www.doctrine-project.org/jira/browse/DDC-2768) - duplicates [DDC-1471: Unable to read mySQL "DATE" field from mysql db](http://www.doctrine-project.org/jira/browse/DDC-1471) - duplicates [DDC-2487: UnitOfWork::getEntityIdentifier() contains objects when custom mapping types are part of an entity's identity](http://www.doctrine-project.org/jira/browse/DDC-2487) - duplicates [DDC-1780: Function createEntity does't support DateTime identifier](http://www.doctrine-project.org/jira/browse/DDC-1780)
Author
Owner

@doctrinebot commented on GitHub (Nov 6, 2014):

Comment created by cverges-ch:

There is a portable workaround where you create a string-based shadow key that gets updated by Doctrine events:

DateTimeIdTest.php:

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Events as Events;

/****
 * @ORM\Table(name="datetime*id*test")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class DateTimeIdTest
{
    public $when;

    /****
     * @ORM\Column(name="when", type="string")
     * @ORM\Id
     */
    public $whenKey;

    public function **construct()
    {
        $this->when = new \DateTime();
    }

    /****
     * @Events\PrePersist
     * @Events\PreUpdate
     */
    public function syncWhenToWhenKey(LifecycleEventArgs $event)
    {
        $entityManager = $event->getEntityManager();
        $connection = $entityManager->getConnection();
        $platform = $connection->getDatabasePlatform();

        $this->whenKey = $when->format($platform->getDateTimeFormatString());
    }

    /****
     * @Events\PostLoad
     */
    public function syncWhenKeyToWhen(LifecycleEventArgs $event)
    {
        $entityManager = $event->getEntityManager();
        $connection = $entityManager->getConnection();
        $platform = $connection->getDatabasePlatform();

        $this->when = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $this->whenKey);
    }
}
@doctrinebot commented on GitHub (Nov 6, 2014): Comment created by cverges-ch: There is a portable workaround where you create a string-based shadow key that gets updated by Doctrine events: **DateTimeIdTest.php:** ``` use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Events as Events; /**** * @ORM\Table(name="datetime*id*test") * @ORM\Entity * @ORM\HasLifecycleCallbacks */ class DateTimeIdTest { public $when; /**** * @ORM\Column(name="when", type="string") * @ORM\Id */ public $whenKey; public function **construct() { $this->when = new \DateTime(); } /**** * @Events\PrePersist * @Events\PreUpdate */ public function syncWhenToWhenKey(LifecycleEventArgs $event) { $entityManager = $event->getEntityManager(); $connection = $entityManager->getConnection(); $platform = $connection->getDatabasePlatform(); $this->whenKey = $when->format($platform->getDateTimeFormatString()); } /**** * @Events\PostLoad */ public function syncWhenKeyToWhen(LifecycleEventArgs $event) { $entityManager = $event->getEntityManager(); $connection = $entityManager->getConnection(); $platform = $connection->getDatabasePlatform(); $this->when = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $this->whenKey); } } ```
Author
Owner

@doctrinebot commented on GitHub (Nov 7, 2014):

Issue was closed with resolution "Duplicate"

@doctrinebot commented on GitHub (Nov 7, 2014): Issue was closed with resolution "Duplicate"
Author
Owner

@peter-gribanov commented on GitHub (Jan 12, 2018):

Will this bug fix?

@peter-gribanov commented on GitHub (Jan 12, 2018): Will this bug fix?
Author
Owner

@Ocramius commented on GitHub (Jan 12, 2018):

@peter-gribanov I already marked this as duplicate

@Ocramius commented on GitHub (Jan 12, 2018): @peter-gribanov I already marked this as `duplicate`
Author
Owner

@peter-gribanov commented on GitHub (Jan 12, 2018):

@Ocramius Thanks. I noticed, but where is the original issue? Is it only in your Jira?

@peter-gribanov commented on GitHub (Jan 12, 2018): @Ocramius Thanks. I noticed, but where is the original issue? Is it only in your Jira?
Author
Owner

@Ocramius commented on GitHub (Jan 12, 2018):

@peter-gribanov all issues were copied over and auto-closed: you can search for them by the identifier (for example DDC-123456) in this repo

@Ocramius commented on GitHub (Jan 12, 2018): @peter-gribanov all issues were copied over and auto-closed: you can search for them by the identifier (for example `DDC-123456`) in this repo
Author
Owner

@peter-gribanov commented on GitHub (Jan 12, 2018):

This issue related:

@Ocramius All tasks that i can see is are already closed, but the bug is not resolved. And yet, where can i find out and monitor the status of this bug?

@peter-gribanov commented on GitHub (Jan 12, 2018): This issue related: * DDC-2768 #3514 * DDC-2912 #3671 * DDC-1471 #275 * DDC-1061 #1653 * DDC-2487 #3206 * DDC-1780 #2433 @Ocramius All tasks that i can see is are already closed, but the bug is not resolved. And yet, where can i find out and monitor the status of this bug?
Author
Owner

@drupol commented on GitHub (Dec 7, 2020):

I'm also looking for a solution.

@drupol commented on GitHub (Dec 7, 2020): I'm also looking for a solution.
Author
Owner

@beberlei commented on GitHub (Dec 7, 2020):

@drupol You could look into using Chronos for Date intead of native date types. They have a __toString and that should make this work: https://packagist.org/packages/warhuhn/chronos-doctrine

@beberlei commented on GitHub (Dec 7, 2020): @drupol You could look into using Chronos for Date intead of native date types. They have a __toString and that should make this work: https://packagist.org/packages/warhuhn/chronos-doctrine
Author
Owner

@drupol commented on GitHub (Dec 7, 2020):

Thanks @beberlei, I will give it a try again.

@drupol commented on GitHub (Dec 7, 2020): Thanks @beberlei, I will give it a try again.
Author
Owner

@drupol commented on GitHub (Dec 7, 2020):

Dear @beberlei , I tried here: https://github.com/drupol/doctrine-date-as-id/pull/2 but apparently, it's the same issue.

@drupol commented on GitHub (Dec 7, 2020): Dear @beberlei , I tried here: https://github.com/drupol/doctrine-date-as-id/pull/2 but apparently, it's the same issue.
Author
Owner

@beberlei commented on GitHub (Dec 7, 2020):

Please read the error csrefully, your code still has a DateTime object there you need a Chronos instance

@beberlei commented on GitHub (Dec 7, 2020): Please read the error csrefully, your code still has a DateTime object there you need a Chronos instance
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4174