AlgoliaIndexer for DataObject requires object|string but receiving an array

Silverstripe Version: 10.0.0

I am using the SilverStripe Algolia Module and it works well except for an error I am receiving 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 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();
    }
}

How should I have added the manual index to my dataobject to avoid this error? I can’t find any examples of how to do this :slight_smile:

It looks like you are using the silverstripe/algolia module to index your data objects in Algolia. If you are receiving an error when calling $this->indexInAlgolia() after creating a Provider data object, there could be a few reasons for this. Here are some potential causes and solutions to consider:

  1. Make sure that you have correctly implemented the AlgoliaSearchable extension on your Provider data object. The extension should be added using the add_extension method in your data object’s requireDefaultRecords method, like this:
public function requireDefaultRecords()
{
    parent::requireDefaultRecords();

    $this->add_extension(AlgoliaSearchable::class);
}
  1. Make sure that you have correctly configured the Algolia API credentials in your SilverStripe configuration. The silverstripe/algolia module requires that you set the ALGOLIA_APPLICATION_ID and ALGOLIA_API_KEY constants in your mysite/_config/config.yml file.
  2. Check for any errors in the PHP error log. If there are any syntax errors or other issues in your code, they may be preventing the indexInAlgolia() method from being called properly.

To manually index a data object in Algolia using the silverstripe/algolia module, you can use the indexInAlgolia method on the data object instance.

Here is an example of how you can do this:

use SilverStripe\ORM\DataObject;
use SilverStripe\Algolia\Algolia;
use SilverStripe\Algolia\Interfaces\AlgoliaIndexable;

class MyDataObject extends DataObject implements AlgoliaIndexable
{
    // ...

    public function onAfterWrite()
    {
        parent::onAfterWrite();

        // Index the object in Algolia
        $this->indexInAlgolia();
    }
}

In this example, the onAfterWrite method is implemented to index the data object in Algolia every time it is created or updated. You can also use this method to update the object’s index when it is deleted by calling $this->deleteFromAlgolia().