Skip to content

Commit a3da3d7

Browse files
authored
Merge pull request #9159 from ThomasLandauer/patch-10
Merging Lifecycle Callbacks code samples for PHP + XML + YAML
2 parents 6f194ee + 5e6608b commit a3da3d7

File tree

1 file changed

+50
-109
lines changed

1 file changed

+50
-109
lines changed

docs/en/reference/events.rst

Lines changed: 50 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -290,133 +290,74 @@ specific to a particular entity class's lifecycle.
290290

291291
Note that Licecycle Callbacks are not supported for Embeddables.
292292

293-
.. code-block:: php
293+
.. configuration-block::
294294

295-
<?php
295+
.. code-block:: php
296296
297-
/** @Entity @HasLifecycleCallbacks */
298-
class User
299-
{
300-
// ...
297+
<?php
301298
302299
/**
303-
* @Column(type="string", length=255)
300+
* @Entity
301+
* @HasLifecycleCallbacks // Not needed for XML and YAML mapping
304302
*/
305-
public $value;
306-
307-
/** @Column(name="created_at", type="string", length=255) */
308-
private $createdAt;
309-
310-
/** @PrePersist */
311-
public function doStuffOnPrePersist()
303+
class User
312304
{
313-
$this->createdAt = date('Y-m-d H:i:s');
314-
}
305+
// ...
315306
316-
/** @PrePersist */
317-
public function doOtherStuffOnPrePersist()
318-
{
319-
$this->value = 'changed from prePersist callback!';
320-
}
307+
/**
308+
* @Column(type="string", length=255)
309+
*/
310+
public $value;
321311
322-
/** @PostPersist */
323-
public function doStuffOnPostPersist()
324-
{
325-
$this->value = 'changed from postPersist callback!';
326-
}
312+
/** @PrePersist */
313+
public function doStuffOnPrePersist()
314+
{
315+
$this->createdAt = date('Y-m-d H:i:s');
316+
}
327317
328-
/** @PostLoad */
329-
public function doStuffOnPostLoad()
330-
{
331-
$this->value = 'changed from postLoad callback!';
332-
}
318+
/** @PrePersist */
319+
public function doOtherStuffOnPrePersist()
320+
{
321+
$this->value = 'changed from prePersist callback!';
322+
}
333323
334-
/** @PreUpdate */
335-
public function doStuffOnPreUpdate()
336-
{
337-
$this->value = 'changed from preUpdate callback!';
324+
/** @PostLoad */
325+
public function doStuffOnPostLoad()
326+
{
327+
$this->value = 'changed from postLoad callback!';
328+
}
338329
}
339-
}
340-
341-
Note that the methods set as lifecycle callbacks need to be public and,
342-
when using these annotations, you have to apply the
343-
``@HasLifecycleCallbacks`` marker annotation on the entity class.
344-
345-
If you want to register lifecycle callbacks from YAML or XML you
346-
can do it with the following.
347-
348-
.. code-block:: yaml
349-
350-
User:
351-
type: entity
352-
fields:
353-
# ...
354-
name:
355-
type: string(50)
356-
lifecycleCallbacks:
357-
prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersist ]
358-
postPersist: [ doStuffOnPostPersist ]
359-
360-
In YAML the ``key`` of the lifecycleCallbacks entry is the event that you
361-
are triggering on and the value is the method (or methods) to call. The allowed
362-
event types are the ones listed in the previous Lifecycle Events section.
363-
364-
XML would look something like this:
365-
366-
.. code-block:: xml
367-
368-
<?xml version="1.0" encoding="UTF-8"?>
369-
370-
<doctrine-mapping xmlns="https://doctrine-project.org/schemas/orm/doctrine-mapping"
371-
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
372-
xsi:schemaLocation="https://doctrine-project.org/schemas/orm/doctrine-mapping
373-
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
374-
375-
<entity name="User">
376-
377-
<lifecycle-callbacks>
378-
<lifecycle-callback type="prePersist" method="doStuffOnPrePersist"/>
379-
<lifecycle-callback type="postPersist" method="doStuffOnPostPersist"/>
380-
</lifecycle-callbacks>
381-
382-
</entity>
383-
384-
</doctrine-mapping>
385-
386-
In XML the ``type`` of the lifecycle-callback entry is the event that you
387-
are triggering on and the ``method`` is the method to call. The allowed event
388-
types are the ones listed in the previous Lifecycle Events section.
330+
.. code-block:: xml
389331
390-
When using YAML or XML you need to remember to create public methods to match the
391-
callback names you defined. E.g. in these examples ``doStuffOnPrePersist()``,
392-
``doOtherStuffOnPrePersist()`` and ``doStuffOnPostPersist()`` methods need to be
393-
defined on your ``User`` model.
332+
<?xml version="1.0" encoding="UTF-8"?>
394333
395-
.. code-block:: php
334+
<doctrine-mapping xmlns="https://doctrine-project.org/schemas/orm/doctrine-mapping"
335+
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
336+
xsi:schemaLocation="https://doctrine-project.org/schemas/orm/doctrine-mapping
337+
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
396338
397-
<?php
398-
// ...
399-
400-
class User
401-
{
402-
// ...
339+
<entity name="User">
403340
404-
public function doStuffOnPrePersist()
405-
{
406-
// ...
407-
}
341+
<lifecycle-callbacks>
342+
<lifecycle-callback type="prePersist" method="doStuffOnPrePersist"/>
343+
<lifecycle-callback type="prePersist" method="doOtherStuffOnPrePersist"/>
344+
<lifecycle-callback type="postLoad" method="doStuffOnPostLoad"/>
345+
</lifecycle-callbacks>
408346
409-
public function doOtherStuffOnPrePersist()
410-
{
411-
// ...
412-
}
347+
</entity>
413348
414-
public function doStuffOnPostPersist()
415-
{
416-
// ...
417-
}
418-
}
349+
</doctrine-mapping>
350+
.. code-block:: yaml
419351
352+
User:
353+
type: entity
354+
fields:
355+
# ...
356+
value:
357+
type: string(255)
358+
lifecycleCallbacks:
359+
prePersist: [ doStuffOnPrePersist, doOtherStuffOnPrePersist ]
360+
postLoad: [ doStuffOnPostLoad ]
420361
421362
Lifecycle Callbacks Event Argument
422363
----------------------------------

0 commit comments

Comments
 (0)