Skip to content

Commit 822ddcf

Browse files
authored
Merge pull request #6568 from cakephp/after-marshall-event
Add docs for afterMarshal event.
2 parents 77d29b8 + 321392a commit 822ddcf

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

en/appendices/4-1-migration-guide.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ Log
137137
* Log messages can now contain ``{foo}`` style placeholders. These placeholders
138138
will be replaced by values from the ``$context`` parameter if available.
139139

140+
ORM
141+
---
142+
143+
* The ORM now triggers an ``Model.afterMarshal`` event which is triggered after
144+
each entity is marshaled from request data.
145+
140146
TestSuite
141147
---------
142148

en/orm/saving-data.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,34 @@ Moreover, the data in ``beforeMarshal`` is a copy of the passed data. This is
636636
because it is important to preserve the original user input, as it may be used
637637
elsewhere.
638638

639+
Modifying Entities After Updating From Request Data
640+
---------------------------------------------------
641+
642+
The ``Model.afterMarshal`` event allows you to modify entities after they have
643+
been created or updated from request data. It can be useful to apply additional
644+
validation logic that you cannot easily express through Validator methods::
645+
646+
// Include use statements at the top of your file.
647+
use Cake\Event\EventInterface;
648+
use Cake\ORM\EntityInterface;
649+
use ArrayObject;
650+
651+
// In a table or behavior class
652+
public function afterMarshal(
653+
EventInterface $event,
654+
EntityInterface $entity,
655+
ArrayObject $data,
656+
ArrayObject $options
657+
) {
658+
// Don't accept people who have a name starting with J on the 20th
659+
// of each month.
660+
if (substr($entity->name, 1) == 'J' && date('d') === 20) {
661+
$entity->setError('name', 'No J people today sorry.');
662+
}
663+
}
664+
665+
.. versionadded:: 4.1.0
666+
639667
Validating Data Before Building Entities
640668
----------------------------------------
641669

en/orm/table-objects.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Event List
139139

140140
* ``Model.initialize``
141141
* ``Model.beforeMarshal``
142+
* ``Model.afterMarshal``
142143
* ``Model.beforeFind``
143144
* ``Model.buildValidator``
144145
* ``Model.buildRules``
@@ -196,6 +197,17 @@ beforeMarshal
196197
The ``Model.beforeMarshal`` event is fired before request data is converted
197198
into entities. See the :ref:`before-marshal` documentation for more information.
198199

200+
afterMarshal
201+
-------------
202+
203+
.. php:method:: afterMarshal(EventInterface $event, EntityInterface $entity, ArrayObject $data, ArrayObject $options)
204+
205+
The ``Model.afterMarshal`` event is fired after request data is converted
206+
into entities. Event handlers will get the converted entities, original request
207+
data and the options provided to the ``patchEntity()`` or ``newEntity()`` call.
208+
209+
.. versionadded:: 4.1.0
210+
199211
beforeFind
200212
----------
201213

0 commit comments

Comments
 (0)