Answer the question
In order to leave comments, you need to log in
How to set default value for DOCTRINE ORM entity?
Hello
Can you tell me if it is possible to set an SQL statement as a default value using Doctrine ORM?
It is necessary that when creating the Operation entity in the number field, the result of executing the sql expression is saved - CONCAT('T', LPAD(nextval('number_id_seq')::text, 12, '0'))
Something like this:
/**
* @ORM\Entity(repositoryClass=OperationRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
class Operation {
/**
* @ORM\PrePersist()
*/
public function prePersist(): void {
if (empty($this->number)){
$this->number = new Expression("CONCAT('T', LPAD(nextval('number_id_seq')::text, 12, '0'))");
}
if (empty($this->createdAt)){
$this->createdAt = new Expression('NOW()');
}
}
}
Answer the question
In order to leave comments, you need to log in
It is possible, throughoptions={"default"="now()"}
/**
* @ORM\Column(name="date", type="datetime_immutable", nullable=false, options={"default"="now()"})
*/
private \DateTimeImmutable $date;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question