# How to share info between collections.save.before and collections.save.after?

**URL:** <https://discourse.getcockpit.com/t/how-to-share-info-between-collections-save-before-and-collections-save-after/1778>\
**Category:** Support\
**Created:** [January 6, 2021, 6:45pm UTC](https://discourse.getcockpit.com/t/how-to-share-info-between-collections-save-before-and-collections-save-after/1778 "2021-01-06T18:45:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixelkarma](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/pixelkarma/32/748_2.png) [@pixelkarma](https://discourse.getcockpit.com/u/pixelkarma)\
**Post date:** [January 6, 2021, 6:45pm UTC](https://discourse.getcockpit.com/t/how-to-share-info-between-collections-save-before-and-collections-save-after/1778/1 "2021-01-06T18:45:59Z")

</div>

Is there a best practice for sharing information between **collections.save.before** and **collections.save.after** so that I can compare the updated data?

Example: If a Collection called **Users** has a field called **username** and I want to execute something if the value is updated. Like an email indicating their username changed.

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [January 9, 2021, 3:25pm UTC](https://discourse.getcockpit.com/t/how-to-share-info-between-collections-save-before-and-collections-save-after/1778/2 "2021-01-09T15:25:38Z")

</div>

You can nest these events. I wrote a quick example (not tested):

```php
$app->on('collections.save.before', function($name, &$entry, $isUpdate) {

    // new entries don't need comparison
    if (!$isUpdate) return;

    // fetch current entry
    $origEntry = $this->module('collections')->findOne($name, ['_id' => $entry['_id']);

    // add event to '...after' queue, that is aware of $origEntry
    $this->on('collections.save.after', function($name, &$entry, $isUpdate) use($origEntry) {

        // compare original entry with current entry and do great things

    }, -9999); // low priority to fire after all other 'collections.save.after' events

}, 9999); // high priority to fire before all other 'collections.save.before' events

```
