From 7384d011e93aa22ab283782de12b33b0d77b4ef7 Mon Sep 17 00:00:00 2001 From: micmania1 Date: Mon, 24 Nov 2014 22:45:21 +0000 Subject: [PATCH] FIX DataDifferencer was trying to compare fields, even if the fields didn't exist causing an error. --- model/DataDifferencer.php | 45 +++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/model/DataDifferencer.php b/model/DataDifferencer.php index 314847d65..36c0b4643 100644 --- a/model/DataDifferencer.php +++ b/model/DataDifferencer.php @@ -77,28 +77,33 @@ class DataDifferencer extends ViewableData { $diffed = clone $this->toRecord; $fields = array_keys($this->toRecord->toMap()); } - - $hasOnes = $this->fromRecord->has_one(); - + + $hasOnes = array_merge($this->fromRecord->has_one(), $this->toRecord->has_one()); + // Loop through properties foreach($fields as $field) { if(in_array($field, $this->ignoredFields)) continue; if(in_array($field, array_keys($hasOnes))) continue; - + if(!$this->fromRecord) { $diffed->setField($field, "" . $this->toRecord->$field . ""); - } else if($this->fromRecord->$field != $this->toRecord->$field) { + } else if($this->fromRecord->$field != $this->toRecord->$field) { $diffed->setField($field, Diff::compareHTML($this->fromRecord->$field, $this->toRecord->$field)); } } - + // Loop through has_one foreach($hasOnes as $relName => $relSpec) { if(in_array($relName, $this->ignoredFields)) continue; - + + // Create the actual column name $relField = "{$relName}ID"; - $relObjTo = $this->toRecord->$relName(); - + $toTitle = ''; + if($this->toRecord->hasMethod($relName)) { + $relObjTo = $this->toRecord->$relName(); + $toTitle = $relObjTo->hasMethod('Title') || $relObjTo->hasField('Title') ? $relObjTo->Title : ''; + } + if(!$this->fromRecord) { if($relObjTo) { if($relObjTo instanceof Image) { @@ -107,28 +112,32 @@ class DataDifferencer extends ViewableData { // not playing nice with mocked images $diffed->setField($relName, "" . $relObjTo->getTag() . ""); } else { - $diffed->setField($relField, "" . $relObjTo->Title() . ""); + $diffed->setField($relField, "" . $toTitle . ""); } } } else if($this->fromRecord->$relField != $this->toRecord->$relField) { - $relObjFrom = $this->fromRecord->$relName(); - if($relObjFrom instanceof Image) { + $fromTitle = ''; + if($this->fromRecord->hasMethod($relName)) { + $relObjFrom = $this->fromRecord->$relName(); + $fromTitle = $relObjFrom->hasMethod('Title') || $relObjFrom->hasField('Title') ? $relObjFrom->Title : ''; + } + if(isset($relObjFrom) && $relObjFrom instanceof Image) { // TODO Use CMSThumbnail (see above) $diffed->setField( - // Using relation name instead of database column name, because of FileField etc. - $relName, + // Using relation name instead of database column name, because of FileField etc. + $relName, Diff::compareHTML($relObjFrom->getTag(), $relObjTo->getTag()) ); } else { + // Set the field. $diffed->setField( - $relField, - Diff::compareHTML($relObjFrom->getTitle(), $relObjTo->getTitle()) + $relField, + Diff::compareHTML($fromTitle, $toTitle) ); } - } } - + return $diffed; }