PHP

Doctrine Date Modified On Update

Doctrine is a set of PHP class libraries that are focused on database storage and database management. The main projects with Doctrine are the ORM (Object Relational Manager) and DBAL (Database Abstraction Layer). Using these two projects Doctrine allows you to build up database entities in PHP code that will represent your application's database structure.

Having PHP classes to construct your database structure means that you can use fully object oriented code to construct the Database queries, this provides developers with a powerful alternative to SQL that maintains flexibility without requiring unnecessary code duplication.

As the tables are defined in PHP classes you can setup a number of different actions to run in the code. There are number of different events that you can already make use of in Doctrine which can be found here.

Doctrine Events

In this tutorial we are going to look at using the PrePersist and the PreUpdate to automatically update a date modified column in your database table. This is common for most tables that change often you will want to keep track of that change by creating a column with the date and time of when the row was last updated.

To do this we need to change a new method on the entity and simply add the PrePersist and the PreUpdate events in the annotation of the method.

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function updateModifiedDatetime()
{

}

The code we place inside this method will run when the entity is either being persisted or being updated, therefore we just need to set the $dateModified property to the current date and time by using the DateTime object.

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function updateModifiedDatetime()
{
    $this->setDateModified(new \DateTime());
}

That's it, that's all the code you need to do to achieve this task which will save you creating an update function and manually setting the date modified before updating the entity.

Below is the full code of a test entity you can use to try out these events.

<?php
namespace Test\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Entity
 *
 * @ORM\Table(name="entity")
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class Entity
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** @ORM\Column(name="datemodified", type="datetime") */
    protected $dateModified;

    public function __construct()
    {
        if ($this->getDateModified() == null) {
            $this->setDateModified(new \DateTime());
        }
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function updateModifiedDatetime()
    {
        $this->setDateModified(new \DateTime());
    }

    /**
     * Set dateModified
     *
     * @param \DateTime $dateModified
     * @return User
     */
    public function setDateModified($dateModified)
    {
        $this->dateModified = $dateModified;

        return $this;
    }

    /**
     * Get dateModified
     *
     * @return \DateTime 
     */
    public function getDateModified()
    {
        return $this->dateModified;
    }
}

Events Not Triggering

I've had problems with the doctrine events of OnPrePersist and OnPreUpdate not triggering and therefore never running the code I need to be ran when persisting the object to the database.

I've found there can be a number of reasons for this one of found is that Doctrine will only run the OnPreUpdate event if the data on the entity actually changes, if your data is exactly the same then the event is never ran.

Another gotcha I've found is forgetting about is the annotation of @HasLifecycleCallbacks which tells doctrine that this entity has lifecycle events for doctrine to keep track of.

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class YourEntity
{

}
Back to top