From 95cddd001489041fd018804ac686e3532d11ccbb Mon Sep 17 00:00:00 2001 From: Jake Bentvelzen Date: Thu, 21 Jun 2018 17:07:29 +1000 Subject: [PATCH] fix(DataChangeRecord): 4.0 branch - Workaround nested array limitation 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) --- code/dataobjects/DataChangeRecord.php | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/code/dataobjects/DataChangeRecord.php b/code/dataobjects/DataChangeRecord.php index 8f28d53..cf8159b 100644 --- a/code/dataobjects/DataChangeRecord.php +++ b/code/dataobjects/DataChangeRecord.php @@ -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 = ''; @@ -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; + } }