Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
BabakMN opened this issue Mar 17, 2017 · 2 comments

Comments

@BabakMN
Copy link

BabakMN commented Mar 17, 2017

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.

@Ocramius
Copy link
Member

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

@ekonoval
Copy link

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants