BetaRide
BetaRide

Reputation: 16844

doctrine 2.0 : use SQL timestamp

I'm looking for a way to make doctrine using TIMESTAMP instead of DATETIME for MySql.

Additionaly I need to set ON UPDATE CURRENT_TIMESTAMP and CURRENT_TIMESTAMP as default values.

I would like to have the possibility to have all this code in PHP annotations to have everything in one central place.

How can I do that?

Upvotes: 14

Views: 18751

Answers (8)

sixty-nine
sixty-nine

Reputation: 147

/**
 * @var \DateTime
 * @ORM\Column(type="datetime", columnDefinition="timestamp default current_timestamp")
 */
protected $createdAt;

/**
 * @var \DateTime
 * @ORM\Column(type="datetime", columnDefinition="timestamp default current_timestamp on update current_timestamp")
 */
protected $updatedAt;

Upvotes: 3

iloo
iloo

Reputation: 986

As suggested by Daniel Criconet,

@ORM\Column(type="datetime", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")

will make that particular column in the corresponding MySQL table become TIMESTAMP instead of DATETIME.

For YAML version, see the original answer.

Upvotes: 4

user3281376
user3281376

Reputation: 1

@ORM\Version works only for one property per entity so it is not an option if you need to have more than one timestamp field in a table.

Upvotes: 0

slaszu
slaszu

Reputation: 1

/** * @Column(type="datetime", options={"default": 0}) * @version=true */ private $data;

create sql like this: data TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL

tested on version 2.5.4

Upvotes: 0

Polichism
Polichism

Reputation: 224

There is no such thing like using TIMESTAMP in mysql with Doctrine. However, I fixed it myself but have to test it:

  • Create a file: Doctrine\DBAL\Types\TimestampType.php
  • Copy the code from TimeType.php into TimestampType.php
  • Rename 'Time' to 'Timestamp' in the code
  • Create a new constant, and add timestamp to the typesmap in: Doctrine\DBAL\Types\Type.php
  • Create function called getTimestampTypeDeclarationSQL in Doctrine\DBAL\Platforms\AbstractPlatform.php
  • And in that function, return TIMESTAMP

I've used yaml to create my Entities and Proxies, so use in yaml:

type: timestamp

I'm going to test this 'fix'/'workaround' now. I'll let you know.

Upvotes: 4

David Soussan
David Soussan

Reputation: 2736

After hours of searching, I found the answer and I hope this helps anyone else looking for a more satisfactory solution than entity lifecycles and the WRONG column type (because a TIMESTAMP is a particular type with specific behaviours beyond just storing a datetime value)

All you need to do is to add

 * @ORM\Column(type="datetime", nullable=false)
 * @ORM\Version

to your annotation and Doctrine will both create a TIMESTAMP column with DEFAULT CURRENT_TIMESTAMP and then return the actual table value as a valid \DateTime object.

CTOP (Credits to Original Poster)

Upvotes: 28

Saurabh Chandra Patel
Saurabh Chandra Patel

Reputation: 13604

/** @ORM\Column(type="datetime", nullable=false ) */
    protected $created;

this will create timestamp and return datetime object

 public function __construct()
        {
            $this->created = new \DateTime();

        }

Upvotes: 1

claytond
claytond

Reputation: 1099

@Polichism provided the right logic, but the patch wasn't accepted. Instead, users were directed to add a custom type. I've implemented that by combining:

The final versions can be found on GitHub:

NOTE: I've linked to specific commits to ensure that the links remain valid if the file is later removed. Use the history to check for newer versions.

Upvotes: 2

Related Questions