diff --git a/src/TagField.php b/src/TagField.php index 2a81577..f2a715b 100644 --- a/src/TagField.php +++ b/src/TagField.php @@ -566,6 +566,9 @@ public function saveInto(DataObjectInterface $record) $fieldName = $this->getName(); $values = $this->getValueArray(); + // We need to extract IDs as in some cases (Relation) we are unable to use the value field + $ids = []; + if (!$values) { $values = []; } @@ -585,6 +588,7 @@ public function saveInto(DataObjectInterface $record) continue; } + $ids[] = $tag->ID; $cleanValues[] = $tag->{$valueField}; } @@ -595,7 +599,7 @@ public function saveInto(DataObjectInterface $record) if ($relation instanceof Relation) { // Save values into relation - $relation->setByIDList(array_filter($cleanValues ?? [])); + $relation->setByIDList(array_filter($ids ?? [])); } elseif ($this->getAllowRawValue()) { // Store raw data without serialisation $record->{$fieldName} = $cleanValues; @@ -607,8 +611,23 @@ public function saveInto(DataObjectInterface $record) // ... JSON-encoded string for other fields : $this->stringEncode(array_filter(array_values($cleanValues))); } else { - $record->{$fieldName} = $tag && $tag->{$valueField} - ? $tag->{$valueField} + // Detect has one as this case needs ID as opposed to custom value + $relations = $record->hasOne(); + $hasOneDetected = false; + + foreach ($relations as $relationName => $relationTarget) { + $foreignKey = $relationName . 'ID'; + + if ($foreignKey === $fieldName) { + $hasOneDetected = true; + + break; + } + } + + $targetField = $hasOneDetected ? 'ID' : $valueField; + $record->{$fieldName} = $tag && $tag->{$targetField} + ? $tag->{$targetField} : null; } } @@ -708,7 +727,7 @@ protected function getTags($term) // Map into a distinct list $items = []; - foreach ($list->getGenerator() as $record) { + foreach ($list as $record) { $value = $record->{$valueField}; $items[$value] = [ 'Title' => $record->{$titleField}, diff --git a/tests/Stub/TagFieldTestBlogPost.php b/tests/Stub/TagFieldTestBlogPost.php index d6db3dd..4e09e65 100644 --- a/tests/Stub/TagFieldTestBlogPost.php +++ b/tests/Stub/TagFieldTestBlogPost.php @@ -4,8 +4,14 @@ use SilverStripe\Dev\TestOnly; use SilverStripe\ORM\DataObject; -use SilverStripe\TagField\Tests\Stub\TagFieldTestBlogTag; +use SilverStripe\ORM\ManyManyList; +/** + * @property string $Content + * @property int $PrimaryTagID + * @method TagFieldTestBlogTag PrimaryTag() + * @method ManyManyList|TagFieldTestBlogTag[] Tags() + */ class TagFieldTestBlogPost extends DataObject implements TestOnly { private static $table_name = 'TagFieldTestBlogPost'; diff --git a/tests/Stub/TagFieldTestBlogTag.php b/tests/Stub/TagFieldTestBlogTag.php index e01ea4b..9a8918e 100644 --- a/tests/Stub/TagFieldTestBlogTag.php +++ b/tests/Stub/TagFieldTestBlogTag.php @@ -4,8 +4,11 @@ use SilverStripe\Dev\TestOnly; use SilverStripe\ORM\DataObject; -use SilverStripe\TagField\Tests\Stub\TagFieldTestBlogPost; +use SilverStripe\ORM\ManyManyList; +/** + * @method ManyManyList|TagFieldTestBlogPost[] BlogPosts() + */ class TagFieldTestBlogTag extends DataObject implements TestOnly { private static $table_name = 'TagFieldTestBlogTag';