Skip to content

Commit

Permalink
phalcon#15571 - Added dirtyState to serialization in Phalcon\Mvc\Model
Browse files Browse the repository at this point in the history
  • Loading branch information
Aziz Muzafarov committed Nov 9, 2021
1 parent a12f46f commit df8939d
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions phalcon/Mvc/Model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -2635,36 +2635,31 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
/**
* Use the standard serialize function to serialize the array data
*/
var attributes, snapshot, manager;
var attributes, manager, dirtyState, snapshot = null;

let attributes = this->toArray(),
dirtyState = this->dirtyState,
manager = <ManagerInterface> this->getModelsManager();

if manager->isKeepingSnapshots(this) {
if manager->isKeepingSnapshots(this) && this->snapshot != null && attributes != this->snapshot {
let snapshot = this->snapshot;

/**
* If attributes is not the same as snapshot then save snapshot too
*/
if snapshot != null && attributes != snapshot {
return serialize(
[
"_attributes": attributes,
"snapshot": snapshot
]
);
}
}

return serialize(attributes);
return serialize(
[
"properties": attributes,
"snapshot": snapshot,
"dirtyState": dirtyState
]
);
}

/**
* Unserializes the object from a serialized string
*/
public function unserialize(var data)
{
var attributes, container, manager, key, value, snapshot;
var attributes, container, manager, key, value, snapshot, properties, dirtyState;

let attributes = unserialize(data);

Expand Down Expand Up @@ -2708,20 +2703,36 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
*/
manager->initialize(this);

if manager->isKeepingSnapshots(this) {
if fetch snapshot, attributes["snapshot"] {
let this->snapshot = snapshot;
let attributes = attributes["_attributes"];
} else {
let this->snapshot = attributes;
/**
* Fetch serialized props
*/
if fetch properties, attributes["properties"] {
/**
* Update the objects properties
*/
for key, value in properties {
let this->{key} = value;
}
} else {
let properties = [];
}

/**
* Fetch serialized dirtyState
*/
if fetch dirtyState, attributes["dirtyState"] {
let this->dirtyState = dirtyState;
}

/**
* Update the objects attributes
* Fetch serialized snapshot when option is active
*/
for key, value in attributes {
let this->{key} = value;
if manager->isKeepingSnapshots(this) {
if fetch snapshot, attributes["snapshot"] {
let this->snapshot = snapshot;
} else {
let this->snapshot = properties;
}
}
}
}
Expand Down

0 comments on commit df8939d

Please sign in to comment.