Skip to content

Reverting

Oliver Musgrove-Wethey edited this page Dec 1, 2016 · 3 revisions

###Revert Merge After merging objects together you can still revert what has happened. Each record contains its predecessor so you can step back through your merge right to the beginning if needed.

After you have called merge() you are returned a Consolidare\Record\Record object. This has 3 public methods:

  • property($property)
  • retrieve()
  • revert()

However we only care about the revert() method at the moment.

When merging, mergeable objects get converted into records. When one record is constructed with a new record passed into it, it follows its given MergeStrategy combining the fields. revert() returns the instance of Consolidare\Record\Record before the merge essentially stepping back or undoing the merge a single step.

If you passed in 3 mergeable objects you would need to call revert() 3 times to get back to the initial value.

If (in the above example) you call revert a 4th time or on an Record object that cant be reverted it will throw a Consolidare\Record\Exception\CantRevertBackFurtherException exception.

$merge = new Consolidare\Merge();

$merge->data('{"id": 10}');
$merge->data(['name' => 'foo', 'email' => 'bar']);
$merge->data(['email' => 'test@test.com']);

$result = $merge->merge(Consolidare\MergeStrategy\MergeStrategyFactory::basic());

$revert = $result->revert();
$revert->retrieve(); // ['id' => 10, 'name' => 'foo', 'email' => 'bar']
Clone this wiki locally