Search code examples
phppostgresqlsymfonydoctrine-ormstofdoctrineextensions

Timestampable does not work with ORM and PostgreSQL database


I added support for Timestampable in my entity as follow: use Gedmo\Timestampable\Traits\TimestampableEntity;. I have updated my DB by running doctrine:schema:update --force but any time I try to insert a new record I get this message:

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "createdat" violates not-null constraint

Why? I'm using latest Symfony 2.5.3 and PostgreSQL 9.2.9. This is the complete error:

An exception occurred while executing 'INSERT INTO usuarios_externos.usuarios (username, username_canonical, email, email_canonical, enabled, salt, password, last_login, locked, expired, expires_at, confirmation_token, password_requested_at, roles, credentials_expired, credentials_expire_at, id, persona, correo_alternativo, telefono, telefono_movil, fax, pagina_web, direccion, deletedAt, createdAt, updatedAt, pais_id, estado_id, municipio_id, ciudad_id, parroquia_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["reynier", "reynier", "reynierpm@gmail.com", "reynierpm@gmail.com", "false", "hhm95if1uog88koggos48csk48k0w80", "sVzbTOHgZzhU92zPHBFsVG3GqV+DO5xvXxvNdC5/GVJ/Hnvlm8rBsNDsIgPKYXdZ4NcnONqXnrOB6UR+lAluAw==", null, "false", "false", null, null, null, "a:0:{}", "false", null, 3, "true", "reynierpm1@gmail.com", "021245678999", "", "", "", "sadasdasd", null, null, null, 23, 1, 1, 1, 22]

Any advice?

Added entity

<?php

use FOS\UserBundle\Model\User as BaseUser;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="usuarios_externos.usuarios", schema="usuarios_externos")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 */
class Usuario extends BaseUser
{

    ....

    /**
     * @var datetime $created
     *
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime")
     */
    private $created;

    /**
     * @var datetime $updated
     *
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(type="datetime")
     */
    private $updated;

    /**
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
     */
    protected $deletedAt;

    ....

    public function getCreated()
    {
        return $this->created;
    }

    public function getUpdated()
    {
        return $this->updated;
    }

    public function setDeletedAt($deletedAt)
    {
        $this->deletedAt = $deletedAt;
    }

    public function getDeletedAt()
    {
        return $this->deletedAt;
    }

}

Solution

  • You should enable timestampable in your config:

    stof_doctrine_extensions:
        orm:
            default:
                timestampable: true