Skip to content

Commit 29ec296

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: [Doctrine] Add attribute example for Lifecycle Callbacks
2 parents 574c453 + a5d2a2d commit 29ec296

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

doctrine/events.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,29 @@ define a callback for the ``prePersist`` Doctrine event:
8080
}
8181
}
8282
83+
.. code-block:: php-attributes
84+
85+
// src/Entity/Product.php
86+
namespace App\Entity;
87+
88+
use Doctrine\ORM\Mapping as ORM;
89+
90+
// When using attributes, don't forget to add #[ORM\HasLifecycleCallbacks]
91+
// to the class of the entity where you define the callback
92+
93+
#[ORM\Entity]
94+
#[ORM\HasLifecycleCallbacks]
95+
class Product
96+
{
97+
// ...
98+
99+
#[ORM\PrePersist]
100+
public function setCreatedAtValue(): void
101+
{
102+
$this->createdAt = new \DateTimeImmutable();
103+
}
104+
}
105+
83106
.. code-block:: yaml
84107
85108
# config/doctrine/Product.orm.yml

0 commit comments

Comments
 (0)