statamic / eloquent-driver

Provides support for storing your Statamic data in a database, rather than flat files.
https://statamic.dev/tips/storing-content-in-a-database
MIT License
104 stars 75 forks source link

Collection and Entry imports not working #173

Closed andrasvati closed 1 year ago

andrasvati commented 1 year ago

When trying to import collections or imports with php please eloquent:import-entries php please eloquent:import-collections no records are imported

It seems like it is looking for items in the database instead of the stache repository.

When i extend the function useDefaultRepositories() in ImportEntries command with \Illuminate\Support\Facades\Facade::clearResolvedInstance(EntryRepositoryContract::class); \Illuminate\Support\Facades\Facade::clearResolvedInstance(CollectionRepositoryContract::class);

private function useDefaultRepositories()
    {
        \Illuminate\Support\Facades\Facade::clearResolvedInstance(EntryRepositoryContract::class);
        \Illuminate\Support\Facades\Facade::clearResolvedInstance(CollectionRepositoryContract::class);

        Statamic::repository(EntryRepositoryContract::class, EntryRepository::class);
        Statamic::repository(CollectionRepositoryContract::class, CollectionRepository::class);

        app()->bind(EntryContract::class, app('statamic.eloquent.entries.entry'));
    }

and in ImportCollections with \Illuminate\Support\Facades\Facade::clearResolvedInstance(CollectionRepositoryContract::class); \Illuminate\Support\Facades\Facade::clearResolvedInstance(CollectionTreeRepositoryContract::class);

private function usingDefaultRepositories(Closure $callback)
    {
        $originalRepo = get_class(app()->make(CollectionRepositoryContract::class));
        $originalTreeRepo = get_class(app()->make(CollectionTreeRepositoryContract::class));
        $originalCollection = get_class(app()->make(CollectionContract::class));

        \Illuminate\Support\Facades\Facade::clearResolvedInstance(CollectionRepositoryContract::class);
        \Illuminate\Support\Facades\Facade::clearResolvedInstance(CollectionTreeRepositoryContract::class);

        Statamic::repository(CollectionRepositoryContract::class, CollectionRepository::class);
        Statamic::repository(CollectionTreeRepositoryContract::class, CollectionTreeRepository::class);
        app()->bind(CollectionContract::class, StacheCollection::class);

        $callback();

        Statamic::repository(CollectionRepositoryContract::class, $originalRepo);
        Statamic::repository(CollectionTreeRepositoryContract::class, $originalTreeRepo);
        app()->bind(CollectionContract::class, $originalCollection);
        Facade::clearResolvedInstance(CollectionRepositoryContract::class);
    }

it begins working.

What could be wrong? I'm using "statamic/cms": "^3.4", (3.4.11) "statamic/eloquent-driver": "^1.2" (1.2.0)

ryanmitchell commented 1 year ago

There have been a number of updates to both these imports in the most recent version (2.1.0). Can you update to that?

andrasvati commented 1 year ago

It requires "statamic/cms": "^4.0.0" as i see, so i can't do it right now without upgrading statamic itself.

ryanmitchell commented 1 year ago

Then unfortunately it would be a case of backporting the bits of code you want need to your previous version. the imports do work in more recent versions (there is a bug on the entries import which theres an open PR to fix).

andrasvati commented 1 year ago

I've upgraded to statamic 4.0 (4.7.0) and eloquent-driver 2.0 (2.1.0) and the same problem exists. Adding the above lines fixes this, altough when having trees, i've got to run the imports in following order to import them too: php please eloquent:import-collections php please eloquent:import-entries php please eloquent:import-collections

(at first the trees aren't imported)

ryanmitchell commented 1 year ago

Thanks for this - I've opened a PR with the code changes required: https://github.com/statamic/eloquent-driver/pull/174

You could get around it by setting your repository to 'file' before importing, but I understand that might not have been obvious.

In terms of the tree import - did nothing import at all? I cant replicate that one on my local install. I would be intending to split the 'tree' import out on its own once we merge https://github.com/statamic/eloquent-driver/pull/133, as at that point the config and data will be seperate.