IDEA: store initial entity state as volatile data #9187

Closed
hypeJunction opened this Issue Dec 4, 2015 · 11 comments

Comments

Projects
None yet
2 participants
@hypeJunction
Contributor

hypeJunction commented Dec 4, 2015

'update' events need to know if entity attributes have been modified to do something useful, e.g. move icons to a different location, if the owner has changed. One way to achieve this is to store all attributes as volatile data when entity has been loaded, and replace them once entity has been saved and all event handlers have been processed.

@mrclay

This comment has been minimized.

Show comment
Hide comment
@mrclay

mrclay Dec 4, 2015

Member

Yeah, I've been thinking this for awhile (see #5604). #9033 might be easier, but would hand over responsibility to plugins to detect changes between two events. Also is a case for event $params.

Member

mrclay commented Dec 4, 2015

Yeah, I've been thinking this for awhile (see #5604). #9033 might be easier, but would hand over responsibility to plugins to detect changes between two events. Also is a case for event $params.

@mrclay

This comment has been minimized.

Show comment
Hide comment
@mrclay

mrclay Dec 4, 2015

Member

Actually this will probably be necessary in any scenario where we allow plugins to detect change.

Member

mrclay commented Dec 4, 2015

Actually this will probably be necessary in any scenario where we allow plugins to detect change.

@hypeJunction

This comment has been minimized.

Show comment
Hide comment
@hypeJunction

hypeJunction Dec 4, 2015

Contributor

In one of my projects I recently implemented a getFootprint method, which returns all object vars. I then serialize that data and create an updated annotation, so there is a history of all changes made to the object (annotation timestamp and owner represent time of change and person making the change). Obviously, I don't care about metadata access/owners, just a set of key value pairs. Perhaps we could implement something similar, and put all object vars to volatile data on load/save.

Contributor

hypeJunction commented Dec 4, 2015

In one of my projects I recently implemented a getFootprint method, which returns all object vars. I then serialize that data and create an updated annotation, so there is a history of all changes made to the object (annotation timestamp and owner represent time of change and person making the change). Obviously, I don't care about metadata access/owners, just a set of key value pairs. Perhaps we could implement something similar, and put all object vars to volatile data on load/save.

@mrclay

This comment has been minimized.

Show comment
Hide comment
@mrclay

mrclay Dec 4, 2015

Member

So your case needs to consider metadata as just part of the attributes. It would be great if Elgg's API had worked identically between attributes/metadata, but here we are.

Member

mrclay commented Dec 4, 2015

So your case needs to consider metadata as just part of the attributes. It would be great if Elgg's API had worked identically between attributes/metadata, but here we are.

@mrclay

This comment has been minimized.

Show comment
Hide comment
@mrclay

mrclay Dec 4, 2015

Member

I'm thinking: In __set(), if there's a GUID, then we copy the attribute into $entity->original_attributes (private). Basically we don't have to populate this for every loaded entity. Duplicate strings don't add much memory, but it's not free.

Member

mrclay commented Dec 4, 2015

I'm thinking: In __set(), if there's a GUID, then we copy the attribute into $entity->original_attributes (private). Basically we don't have to populate this for every loaded entity. Duplicate strings don't add much memory, but it's not free.

@mrclay

This comment has been minimized.

Show comment
Hide comment
@mrclay

mrclay Dec 4, 2015

Member

...if the key is an attribute. I'm not sure we can do this for the metadata.

Member

mrclay commented Dec 4, 2015

...if the key is an attribute. I'm not sure we can do this for the metadata.

@hypeJunction

This comment has been minimized.

Show comment
Hide comment
@hypeJunction

hypeJunction Dec 4, 2015

Contributor

Nah, let's keep it simple. Metadata changes already get more attention than they deserve in event handling.

Contributor

hypeJunction commented Dec 4, 2015

Nah, let's keep it simple. Metadata changes already get more attention than they deserve in event handling.

@hypeJunction

This comment has been minimized.

Show comment
Hide comment
@hypeJunction

hypeJunction Dec 4, 2015

Contributor

Do we then trigger an event once save() succeeded to notify plugins that attributes have changed? Before the update event has fired? Or would plugins listen to update event and use some new method to retrieve original_attributes?

Contributor

hypeJunction commented Dec 4, 2015

Do we then trigger an event once save() succeeded to notify plugins that attributes have changed? Before the update event has fired? Or would plugins listen to update event and use some new method to retrieve original_attributes?

@mrclay

This comment has been minimized.

Show comment
Hide comment
@mrclay

mrclay Dec 4, 2015

Member

I think ideally there would be a cancellable update:before event optionally followed by update:after if not cancelled, and both would receive an array of changes in params.

Member

mrclay commented Dec 4, 2015

I think ideally there would be a cancellable update:before event optionally followed by update:after if not cancelled, and both would receive an array of changes in params.

@mrclay

This comment has been minimized.

Show comment
Hide comment
@mrclay

mrclay Dec 4, 2015

Member

But for BC, simply exposing an API to get the original attributes might be the simplest solution. I think we'd need that in any case and it might be enough to do the job.

Member

mrclay commented Dec 4, 2015

But for BC, simply exposing an API to get the original attributes might be the simplest solution. I think we'd need that in any case and it might be enough to do the job.

@mrclay

This comment has been minimized.

Show comment
Hide comment
@mrclay

mrclay Dec 4, 2015

Member

Starter implementation #9188

Member

mrclay commented Dec 4, 2015

Starter implementation #9188

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

feature(entities): give access to original values of modified attributes
Once an entity has been saved, Elgg tracks changes to its attributes, so that
handlers of the `update` and `update:after` events can see which attributes are
changing. Calling `$entity->getOriginalAttributes()` returns keys of changed
attributes, and the original values as the array values. E.g.:

```php
function my_update_handler($event, $type, ElggEntity $object) {
    $orig = $object->getOriginalAttributes;
    if (array_key_exists('title', $orig)) {
        // the title changed from $orig['title'] to $object->title
    }
}
```

Fixes #9187

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

feature(entities): give access to original values of modified attributes
Once an entity has been saved, Elgg tracks changes to its attributes, so that
handlers of the `update` and `update:after` events can see which attributes are
changing. Calling `$entity->getOriginalAttributes()` returns keys of changed
attributes, and the original values as the array values. E.g.:

```php
function my_update_handler($event, $type, ElggEntity $object) {
    $orig = $object->getOriginalAttributes();
    if (array_key_exists('title', $orig)) {
        // the title changed from $orig['title'] to $object->title
    }
}
```

Fixes #9187

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

feature(entities): give access to original values of modified attributes
Once an entity has been saved, Elgg tracks changes to its attributes, so that
handlers of the `update` and `update:after` events can see which attributes are
changing. Calling `$entity->getOriginalAttributes()` returns keys of changed
attributes, and the original values as the array values. E.g.:

```php
function my_update_handler($event, $type, ElggEntity $object) {
    $orig = $object->getOriginalAttributes();
    if (array_key_exists('title', $orig)) {
        // the title changed from $orig['title'] to $object->title
    }
}
```

Fixes #9187

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 4, 2015

feature(entities): give access to original values of modified attributes
Once an entity has been saved, Elgg tracks changes to its attributes, so that
handlers of the `update` and `update:after` events can see which attributes are
changing. Calling `$entity->getOriginalAttributes()` returns keys of changed
attributes, and the original values as the array values. E.g.:

```php
function my_update_handler($event, $type, ElggEntity $object) {
    $orig = $object->getOriginalAttributes();
    if (array_key_exists('title', $orig)) {
        // the title changed from $orig['title'] to $object->title
    }
}
```

Fixes #9187

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 6, 2015

feature(entities): give access to original values of modified attributes
Once an entity has been saved, Elgg tracks changes to its attributes, so that
handlers of the `update` and `update:after` events can see which attributes are
changing. Calling `$entity->getOriginalAttributes()` returns keys of changed
attributes, and the original values as the array values. E.g.:

```php
function my_update_handler($event, $type, ElggEntity $object) {
    $orig = $object->getOriginalAttributes();
    if (array_key_exists('title', $orig)) {
        // the title changed from $orig['title'] to $object->title
    }
}
```

This also issues a warning if plugins call `save()` on an entity already
currently being saved.

Fixes #9187

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 6, 2015

feature(entities): give access to original values of modified attributes
Once an entity has been saved, Elgg tracks changes to its attributes, so that
handlers of the `update` and `update:after` events can see which attributes are
changing. Calling `$entity->getOriginalAttributes()` returns keys of changed
attributes, and the original values as the array values. E.g.:

```php
function my_update_handler($event, $type, ElggEntity $object) {
    $orig = $object->getOriginalAttributes();
    if (array_key_exists('title', $orig)) {
        // the title changed from $orig['title'] to $object->title
    }
}
```

This also issues a warning if plugins call `save()` on an entity already
currently being saved.

Fixes #9187

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 6, 2015

feature(entities): give access to original values of modified attributes
Once an entity has been saved, Elgg tracks changes to its attributes, so that
handlers of the `update` and `update:after` events can see which attributes are
changing. Calling `$entity->getOriginalAttributes()` returns keys of changed
attributes, and the original values as the array values. E.g.:

```php
function my_update_handler($event, $type, ElggEntity $object) {
    $orig = $object->getOriginalAttributes();
    if (array_key_exists('title', $orig)) {
        // the title changed from $orig['title'] to $object->title
    }
}
```

This also issues a warning if plugins call `save()` on an entity already
currently being saved.

Fixes #9187

mrclay added a commit to mrclay/Elgg-leaf that referenced this issue Dec 8, 2015

feature(entities): give access to original values of modified attributes
Once an entity has been saved, Elgg tracks changes to its attributes, so that
handlers of the `update` and `update:after` events can see which attributes are
changing. Calling `$entity->getOriginalAttributes()` returns keys of changed
attributes, and the original values as the array values. E.g.:

```php
function my_update_handler($event, $type, ElggEntity $object) {
    $orig = $object->getOriginalAttributes();
    if (array_key_exists('title', $orig)) {
        // the title changed from $orig['title'] to $object->title
    }
}
```

Fixes #9187

@mrclay mrclay closed this in #9188 Dec 21, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment