Skip to content

Commit

Permalink
fix(DataChangeRecord): 4.0 branch - Workaround nested array limitatio…
Browse files Browse the repository at this point in the history
…n with DataDifferencer by only json_decode'ing 1 level down. This resolve bugs when you're storing JSON data in a 'Text' DB field via MultiValueField. (#24) (#25)
  • Loading branch information
silbinarywolf committed Jun 21, 2018
1 parent bfea54a commit 95cddd0
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions code/dataobjects/DataChangeRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public function getCMSFields($params = null) {
);

if (strlen($this->Before)) {
$before = Object::create($this->ClassType, json_decode($this->Before, true), true);
$after = Object::create($this->ClassType, json_decode($this->After, true), true);
$before = Object::create($this->ClassType, $this->prepareForDataDifferencer($this->Before), true);
$after = Object::create($this->ClassType, $this->prepareForDataDifferencer($this->After), true);
$diff = DataDifferencer::create($before, $after);

// The solr search service injector dependency causes issues with comparison, since it has public variables that are stored in an array.

$diff->ignoreFields(array('searchService'));
$diffed = $diff->diffedData();
$diffText = '';
Expand Down Expand Up @@ -272,4 +272,21 @@ public function getMemberDetails(){
}
}

/**
* @return array
*/
private function prepareForDataDifferencer($jsonData)
{
// NOTE(Jake): 2018-06-21
//
// Data Differencer cannot handle arrays within an array,
//
// So JSON data that comes from MultiValueField / Text DB fields
// causes errors to be thrown.
//
// So solve this, we simply only decode to a depth of 1. (rather than the 512 default)
//
$jsonData = json_decode($jsonData, true, 1);
return $jsonData;
}
}

0 comments on commit 95cddd0

Please sign in to comment.