I am trying to make an abstract class to extend my entity from in order to give them some basic functionalities such as automatic timestamp.
So I created this abstract class:
/**
* Class Model
* @package AppBundle
* @ORM\MappedSuperclass
*/
abstract class Model
{
/**
* @var \DateTime $created_at
*
* @ORM\Column(type="datetime")
*/
protected $created_at;
/**
* @var \DateTime $updated_at
*
* @ORM\Column(type="datetime")
*/
protected $updated_at;
/**
*
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps()
{
$this->setUpdatedAt(new \DateTime('now'));
if ($this->getCreatedAt() == null) {
$this->setCreatedAt(new \DateTime('now'));
}
}
public function getUpdatedAt()
{
return $this->updated_at;
}
public function setUpdatedAt($updated_at)
{
$this->updated_at = $updated_at;
}
public function getCreatedAt()
{
return $this->created_at;
}
public function setCreatedAt($created_at)
{
$this->created_at = $created_at;
return $this;
}
}
So now, I just need to extend my entity from this class:
class MyEntity extends Model
{
....
}
However, if I want my timestamps to work, I still need to add the @ORM\HasLifecycleCallbacks
on top of MyEntity class to trigger the updatedTimestamps()
method on persist and on update.
I tried to add the annotation on top of my super class but it does not work.
Would someone have a solution to avoid to add the annotation on top of every entity?
I just want to extend my superclass and that's it...
Thank you for your answers and have a nice day!