Skip to content

Timestamps

Mitchell van Wijngaarden edited this page May 10, 2014 · 1 revision

Doctrine 2 doesn't support timestamps created_at and updated_at out of the box. That's why I've added a trait Timestamps which add this functionality without a sweat. When you add this trait and the @HasLifecycleCallbacks() annotation to your entity managing the timestamps will be done automagically.

Do remember that the database table of this entity needs to have the created_at and updated_at columns. If the trait has been added you can simply call the artisan command doctrine:schema:update to update your database schema.

<?php

use Doctrine\ORM\Mapping AS ORM;
use Mitch\LaravelDoctrine\Traits\Timestamps;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @ORM\HasLifecycleCallbacks()
 */
class User
{
    use Timestamps;

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string")
     */
    private $name;
}