studioespresso / craft-scout

Craft Scout provides a simple solution for adding full-text search to your entries. Scout will automatically keep your search indexes in sync with your entries.
MIT License
81 stars 54 forks source link

Type error with `siteId` in v3.3.1 #284

Closed chrismlusk closed 7 months ago

chrismlusk commented 7 months ago

After updating to v3.3.1, I get this error when attempting to access Utilities > Scout Indices:

foreach() argument must be of type array|object, int given

The stack trace refers to line 44 in ScoutUtility.php.

foreach ($engine->scoutIndex->criteria->siteId as $id) {
    $sites[] = Craft::$app->getSites()->getSiteById($id);
}

Here's my scout.php:

function createIndices(): array
{
    $sites = Craft::$app->getSites()->getSitesByGroupId(1);
    $baseLocationIndex = App::parseEnv('$ALGOLIA_LOCATION_INDEX_BASE');
    $baseProviderIndex = App::parseEnv('$ALGOLIA_PROVIDER_INDEX_BASE');

    $indices = [];

    foreach ($sites as $site) {
        $siteId = $site->id;
        $siteLanguage = $site->language;

        $indices[] = ScoutIndex::create($baseLocationIndex . '_' . $siteLanguage)
            ->criteria(function(EntryQuery $query) use ($siteId) {
                return $query
                    ->sectionId(8)
                    ->siteId($siteId);
            })
            ->transformer(new AlgoliaLocationTransformer([
                'siteId' => $siteId,
                'siteLanguage' => $siteLanguage,
            ]));

        $indices[] = ScoutIndex::create($baseProviderIndex . '_' . $siteLanguage)
            ->criteria(function(EntryQuery $query) use ($siteId) {
                return $query
                    ->sectionId(9)
                    ->siteId($siteId);
            })
            ->transformer(new AlgoliaProviderTransformer([
                'siteId' => $siteId,
                'siteLanguage' => $siteLanguage,
            ]));
    }

    return $indices;
}

return [
    'sync' => false,
    'queue' => true,
    'connect_timeout' => 1,
    'batch_size' => 1000,
    'indexRelations' => true,
    'application_id' => App::parseEnv('$ALGOLIA_APPLICATION_ID'),
    'admin_api_key' => App::parseEnv('$ALGOLIA_ADMIN_API_KEY'),
    'search_api_key' => App::parseEnv('$ALGOLIA_SEARCH_API_KEY'),
    'indices' => createIndices(),
];
janhenckens commented 7 months ago

Thanks for catching that @chrismlusk, pushed a fixed and I'll have release out shortly!

janhenckens commented 7 months ago

Released in 3.3.2, can you give that a go? Feel free to reopen this issue should you still have an issue!

chrismlusk commented 7 months ago

Perfect! Thanks for the quick fix.