statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.05k stars 530 forks source link

Multisite: manual entry creation messes up stache #4034

Closed stuartcusackie closed 9 months ago

stuartcusackie commented 3 years ago

Bug Description

I am creating entries via an artisan console command using the entry facade. It's for a scheduled task that pulls posts from an external source periodically.

Entry::make()
            ->collection('posts')
            ->locale('ie')
            ->published(true)
            ->slug(uniqid())
            ->data([
                'title' => $item->title,
                'posted_at' => $item->date->format('Y-m-d H:i')
            ])
            ->set('updated_by', User::all()->first()->id())
            ->set('updated_at', now()->timestamp)
            ->save();

It works fine and entries are created for the 'ie' locale. The problem is that every time view the dashboard, or each time I re-run the command I get these errors:

Error 1: (occurs on second call of my function)

 Error

Call to a member function sites() on null

at xxx\vendor\statamic\cms\src\Stache\Stores\CollectionEntriesStore.php:39
   35▕         }
   36▕
   37▕         if (Site::hasMultiple()) {
   38▕             [$site, $relative] = explode('/', $relative, 2);
➜  39▕             if (! $this->collection()->sites()->contains($site)) {
   40▕                 return false;
   41▕             }
   42▕         }
   43▕

1   xxx\vendor\statamic\cms\src\Stache\Stores\CollectionEntriesStore.php:39
    Statamic\Stache\Stores\CollectionEntriesStore::collection()

2   [internal]:0
    Statamic\Stache\Stores\CollectionEntriesStore::getItemFilter(Object(Symfony\Component\Finder\SplFileInfo))

Error 2: (occurs on third call of my function - I can't get past this error without wiping the entire cache)

  Call to a member function hasStructure() on null

  at xxx\vendor\statamic\cms\src\Stache\Stores\CollectionEntriesStore.php:133
    129▕     }
    130▕
    131▕     protected function removeEntryFromStructure($collection, $id)
    132▕     {
  ➜ 133▕         if (! $collection->hasStructure()) {
    134▕             return;
    135▕         }
    136▕
    137▕         $collection->structure()->trees()->each(function ($tree) use ($id) {

  1   xxx\vendor\statamic\cms\src\Stache\Stores\CollectionEntriesStore.php:128
      Statamic\Stache\Stores\CollectionEntriesStore::removeEntryFromStructure("6ad7bafc-7a4c-44a6-81d2-8d5a0ea708bd")

  2   xxx\vendor\statamic\cms\src\Stache\Stores\Store.php:220
      Statamic\Stache\Stores\CollectionEntriesStore::handleDeletedItem("xxx/content/collections/posts/ie/2606639329436392456.md", "6ad7bafc-7a4c-44a6-81d2-8d5a0ea708bd")

Temporary Solution

It seems I've run into a similar before: https://github.com/statamic/cms/issues/3175

php artisan cache:clear fixes the problem locally and for some reason the problem hasn't presented on a staging domain. Is this something to do with running my artisan command outside of the vagrant box I wonder...

Yes, it seems doing a vagrant ssh before running my command fixes it. Now why is that?? Is cached data being incorrectly created when running directly from my Operating System - Possible Windows filesystem conflict??

Other Clues

Environment

Statamic version: 3.1.29 PHP version: 7.4 Install method (choose one): Existing Laravel app OS: Windows 10

edalzell commented 3 years ago

Is that collection set to "allow" that locale?

stuartcusackie commented 3 years ago

It is indeed. I have also tried allowing all sites on the collection and generating localizations using the Entry->makeLocalisation() function, but I end up with similar errors.

stuartcusackie commented 3 years ago

Think I've stumbled onto the root of the problem. If I vagrant ssh to my homestead box first (Windows) before running my artisan command, then the error does not occur. I've updated the issue with a temporary solution above.

duncanmcclean commented 9 months ago

I haven't used Homestead in a few years but I do remember having issues w/ Laravel here and there when running commands outside of the box vs inside with SSH. Although, can't remember any specifics.

Since this issue is quite old now, hasn't had any recent activity and we haven't heard from others running into the same issue, I'm going to close this issue.

If you're still running into this issue with Homestead, let us know and we can look at re-opening.