DateTime/DateTimeImmutable as an part of composite key #7176

Open
opened 2026-01-22 15:46:07 +01:00 by admin · 5 comments
Owner

Originally created by @Greg0 on GitHub (Jul 10, 2023).

Feature Request

Doctrine doesn't support DateTime/DateTimeImmutable as part of composite primary key.

Q A
BC Break no
Version 2.15.3

Summary

According to use case https://www.doctrine-project.org/projects/doctrine-orm/en/2.15/tutorials/composite-primary-keys.html#use-case-1-dynamic-attributes

With entity mapping as

#[Entity]
class Entity {}

#[Entity]
class EntityAudit
{
    #[Id, ManyToOne(targetEntity: Entity::class, inversedBy: 'audit')]
    private Entity $entity;

    #[Id, Column]
    private DateTimeImmutable $issuedAt;

    #[Column]
    private string $changedKey;

    public function __construct(DateTimeImmutable $issuedAt, string $changedKey, Entity $entity)
    {
        $this->issuedAt = $issuedAt;
        $this->changedKey = $changedKey;
        $this->entity = $entity;
    }
}

Current behaviour

Doctrine throws

Object of class DateTimeImmutable could not be converted to string at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1631

How to reproduce

I've added a test case to show the above use case
https://github.com/doctrine/orm/pull/10830

Expected behavior

Allow use DateTime/DateTimeImmutable at least as a part of the composite key.
According to using Enums as part of composite key, in \Doctrine\ORM\UnitOfWork::getIdHashByIdentifier DateTimeInterface can be casted to string e.g. as ->format(DateTimeInterface::ATOM)

Originally created by @Greg0 on GitHub (Jul 10, 2023). ### Feature Request Doctrine doesn't support `DateTime`/`DateTimeImmutable` as part of composite primary key. | Q | A |------------ | ------ | BC Break | no | Version |2.15.3 #### Summary According to use case https://www.doctrine-project.org/projects/doctrine-orm/en/2.15/tutorials/composite-primary-keys.html#use-case-1-dynamic-attributes With entity mapping as ```php #[Entity] class Entity {} #[Entity] class EntityAudit { #[Id, ManyToOne(targetEntity: Entity::class, inversedBy: 'audit')] private Entity $entity; #[Id, Column] private DateTimeImmutable $issuedAt; #[Column] private string $changedKey; public function __construct(DateTimeImmutable $issuedAt, string $changedKey, Entity $entity) { $this->issuedAt = $issuedAt; $this->changedKey = $changedKey; $this->entity = $entity; } } ``` #### Current behaviour Doctrine throws ``` Object of class DateTimeImmutable could not be converted to string at vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1631 ``` #### How to reproduce I've added a test case to show the above use case https://github.com/doctrine/orm/pull/10830 #### Expected behavior Allow use `DateTime`/`DateTimeImmutable` at least as a part of the composite key. According to using Enums as part of composite key, in `\Doctrine\ORM\UnitOfWork::getIdHashByIdentifier` DateTimeInterface can be casted to string e.g. as `->format(DateTimeInterface::ATOM)`
admin added the New Feature label 2026-01-22 15:46:07 +01:00
Author
Owner

@stof commented on GitHub (Jul 12, 2023):

Allowing DateTimeImmutable might be possible thanks to the new concept of id hash introduced to support enums.

however, supporting DateTime is a no-go due to its mutable nature (if you mutate the DateTime object, you will still have the same object but a different formatted string for the hash, which would break things)

@stof commented on GitHub (Jul 12, 2023): Allowing DateTimeImmutable might be possible thanks to the new concept of id hash introduced to support enums. however, supporting DateTime is a no-go due to its mutable nature (if you mutate the DateTime object, you will still have the same object but a different formatted string for the hash, which would break things)
Author
Owner

@stof commented on GitHub (Jul 12, 2023):

And this is not a bug report, but a feature request.

@stof commented on GitHub (Jul 12, 2023): And this is not a bug report, but a feature request.
Author
Owner

@Greg0 commented on GitHub (Jul 12, 2023):

Should I edit somehow issue to feature request?
Then what should I do next to made this request implemented part of library?

If there are no problem I can try implmenet solution to attached PR with described use case

@Greg0 commented on GitHub (Jul 12, 2023): Should I edit somehow issue to feature request? Then what should I do next to made this request implemented part of library? If there are no problem I can try implmenet solution to attached PR with described use case
Author
Owner

@greg0ire commented on GitHub (Jul 12, 2023):

Should I edit somehow issue to feature request?

No need, I did it for you

as well as a video tutorial showing how to do so yourself for next time 😉

Peek 2023-07-12 23-34

Then what should I do next to made this request implemented part of library?

Send a PR for DateTimeImmutable .

If there are no problem I can try implmenet solution to attached PR with described use case

Sounds great 👍

@greg0ire commented on GitHub (Jul 12, 2023): > Should I edit somehow issue to feature request? No need, I did it for you <details> <summary>as well as a video tutorial showing how to do so yourself for next time :wink: </summary> ![Peek 2023-07-12 23-34](https://github.com/doctrine/orm/assets/657779/53c1455b-c6cb-4bb4-80a8-d96c5b123b06) </details> > Then what should I do next to made this request implemented part of library? Send a PR for `DateTimeImmutable` . > If there are no problem I can try implmenet solution to attached PR with described use case Sounds great 👍
Author
Owner

@Greg0 commented on GitHub (Jul 17, 2023):

@stof @greg0ire PR is ready to check. I hope it will be good enough. There are some github action checks that should be run again to handle changes.

@Greg0 commented on GitHub (Jul 17, 2023): @stof @greg0ire PR is ready to check. I hope it will be good enough. There are some github action checks that should be run again to handle changes.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7176