wilr / silverstripe-algolia

Handles indexing Silverstripe Pages into Algolia and providing results
BSD 3-Clause "New" or "Revised" License
13 stars 15 forks source link

AlgoliaIndexer for DataObject requires object|string but receiving an array #49

Closed earthwytch closed 2 years ago

earthwytch commented 2 years ago

I am receiving an error when I try to call $this->indexInAlgolia() after creating a ‘Provider’ dataobject which is not using the versioning extension.

I have added the following method to my dataobject to manually add the object to the Algolia index when creating/updating as (I think) it said in the instructions:

public function onAfterWrite()
{
        parent::onAfterWrite();
        if ($this->canIndexInAlgolia()) {
            $this->indexInAlgolia();
        }
}

When creating a new Provider I receive the following error:

method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given at /var/www/localhost/htdocs/vendor/wilr/silverstripe-algolia/src/Service/AlgoliaIndexer.php:66

I can temporarily fix the error by hacking the Wilr\SilverStripe\Algolia\Service\AlgoliaIndexer indexItem() method code to check if the result is already an array (which it appears to be):

if (!is_array($fields)) {
    if (method_exists($fields, 'toArray')) {
        $fields = $fields->toArray();
    }
}

I can't work out how I should implement this correctly to avoid the error. Are there any examples I could see, or could the docs be padded out a little to show how to do this? :) Thank you.

(Also posted on the SilverStripe forum here but there were no responses.)

wilr commented 2 years ago

@earthwytch can you provide you exportObjectToAlgolia method on Provider.

It should return a Map instance.