If you insert a new object into database, onPrePersist() gets fired. If you update an existing object, onPreUpdate() gets fired.


Entity


use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="league")
* @ORM\HasLifecycleCallbacks
*/
class League
{
/**
* @ORM\Column(name="created_at", type="datetime", nullable=false)
*/
protected $createdAt;

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

/**
* @ORM\PrePersist
*/
public function onPrePersist()
{
$this->createdAt = new DateTime('now');
}

/**
* @ORM\PreUpdate
*/
public function onPreUpdate()
{
$this->updatedAt = new DateTime('now');
}
}