Reputation: 16844
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
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
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
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
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
Reputation: 224
There is no such thing like using TIMESTAMP
in mysql with Doctrine.
However, I fixed it myself but have to test it:
Doctrine\DBAL\Types\TimestampType.php
TimeType.php
into TimestampType.php
Doctrine\DBAL\Types\Type.php
getTimestampTypeDeclarationSQL
in Doctrine\DBAL\Platforms\AbstractPlatform.php
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
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
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
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