Is it possible to set a field value to a database expression such as CURRENT_TIMESTAMP? #5464

Closed
opened 2026-01-22 15:08:18 +01:00 by admin · 2 comments
Owner

Originally created by @BabakMN on GitHub (Mar 17, 2017).

Originally assigned to: @Ocramius on GitHub.

I'd like to know if it's possible to set a field value to a database value such as CURRENT_TIMESTAMP?

One main benefit is that it allows you to maintain consistent timestamps within a transaction.

For example I would like the "creation date" of a bunch of different objects that get created within a transaction to be exactly the same as each other.

Otherwise you have to pass a specific DateTime instance around, or you will get different timestamps.

Originally created by @BabakMN on GitHub (Mar 17, 2017). Originally assigned to: @Ocramius on GitHub. I'd like to know if it's possible to set a field value to a database value such as CURRENT_TIMESTAMP? One main benefit is that it allows you to maintain consistent timestamps within a transaction. For example I would like the "creation date" of a bunch of different objects that get created within a transaction to be exactly the same as each other. Otherwise you have to pass a specific `DateTime` instance around, or you will get different timestamps.
admin added the ImprovementCan't FixQuestion labels 2026-01-22 15:08:18 +01:00
admin closed this issue 2026-01-22 15:08:18 +01:00
Author
Owner

@Ocramius commented on GitHub (Mar 17, 2017):

This is not supported, as the ORM treats the in-memory state as authoritative, and would otherwise need to re-query data after each insert/update.

In general, you can consider support for db-side generated fields as a feature that will not land in the ORM.

The safest way to currently get a transaction-safe timestamp is to:

  1. Start a transaction
  2. Select the transaction start time
  3. Use that time in a clock object that you use during entity creation/update
  4. Commit/flush

Closing here

@Ocramius commented on GitHub (Mar 17, 2017): This is not supported, as the ORM treats the in-memory state as authoritative, and would otherwise need to re-query data after each insert/update. In general, you can consider support for db-side generated fields as a feature that will not land in the ORM. The safest way to currently get a transaction-safe timestamp is to: 1. Start a transaction 2. Select the transaction start time 3. Use that time in a `clock` object that you use during entity creation/update 4. Commit/flush Closing here
Author
Owner

@ekonoval commented on GitHub (Mar 19, 2020):

But why doctrine commands still keep generating entities like this ?
http://prntscr.com/rj1y4a

Is it a bug?
I'm using 2.7 version

    /**
     * @var DateTime
     *
     * @ORM\Column(name="trans_date", type="datetime", nullable=false, options={"default"="CURRENT_TIMESTAMP"})
     */
    private $date = 'CURRENT_TIMESTAMP';

    /**
     * @var DateTime|null
     *
     * @ORM\Column(name="created_at", type="datetime", nullable=true, options={"default"="CURRENT_TIMESTAMP"})
     */
    private $createdAt = 'CURRENT_TIMESTAMP';
@ekonoval commented on GitHub (Mar 19, 2020): But why doctrine commands still keep generating entities like this ? http://prntscr.com/rj1y4a Is it a bug? I'm using 2.7 version ``` /** * @var DateTime * * @ORM\Column(name="trans_date", type="datetime", nullable=false, options={"default"="CURRENT_TIMESTAMP"}) */ private $date = 'CURRENT_TIMESTAMP'; /** * @var DateTime|null * * @ORM\Column(name="created_at", type="datetime", nullable=true, options={"default"="CURRENT_TIMESTAMP"}) */ private $createdAt = 'CURRENT_TIMESTAMP'; ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5464