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
105 stars 74 forks source link

Folders not appearing when combining file and eloquent #365

Closed jymden closed 6 days ago

jymden commented 6 days ago

Bug description

After creating a folder in an asset container, it disappears from the control panel upon refresh unless a file is uploaded into the folder. The folder exists in the file system or on S3 but is not visible in Statamic. This happens with file-based asset containers, even when a .gitkeep file is present.

So in short. You need to have asset containers and assets set to the same driver (file or eloquent) for empty folders to show up in Statamic.

We have now moved both assets and asset containers to Eloquent, and the issue has been resolved for us. However, we'd like to confirm whether this is a bug or expected behavior.

How to reproduce

  1. Set up Statamic with Eloquent drivers for assets and file driver asset containers, or vice versa,
  2. Create a folder in an asset container from the control panel.
  3. Refresh the control panel page.
  4. Observe that the folder has disappeared from the control panel, but it exists on the disk (e.g., in the file system or on S3).

Logs

No response

Environment

Statamic version: 5.31.0
Eloquent Driver Version: 4.15.2
PHP: 8.2.18
WAMP, Azure, Docker

Filesystem Configuration:
Assets stored using Eloquent driver.
Asset containers using file driver.

Additional details

No response

ryanmitchell commented 6 days ago

Correct, you need to have an asset in the folder for it to be saved. The driver creates the folder structure from the assets so any empty folders are ignored.

jymden commented 6 days ago

Ok. I just found it a bit strange, since it works fine having empty folders (with no assets in them) if you have it like this:

    'asset_containers' => [
        'driver' => 'eloquent',
        'model' => \Statamic\Eloquent\Assets\AssetContainerModel::class,
    ],

    'assets' => [
        'driver' => 'eloquent',
        'model' => \Statamic\Eloquent\Assets\AssetModel::class,
        'asset' => \Statamic\Eloquent\Assets\Asset::class,
    ],

or this:

    'asset_containers' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Assets\AssetContainerModel::class,
    ],

    'assets' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Assets\AssetModel::class,
        'asset' => \Statamic\Eloquent\Assets\Asset::class,
    ],

But not like this:

    'asset_containers' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Assets\AssetContainerModel::class,
    ],

    'assets' => [
        'driver' => 'eloquent',
        'model' => \Statamic\Eloquent\Assets\AssetModel::class,
        'asset' => \Statamic\Eloquent\Assets\Asset::class,
    ],

If you have the combination, the folder will be created, it just turns invisible. And if you try to create it again you get an error since the folder actually exists there but it's not listed in Statamic. If that's the expected behavior I think it's probably a good idea to document it. It was very confusing that the folders just disappeared (since this is not the standard behavior of Statamic. In vanilla you can have empty folders.) Also makes it extremely difficult to arrange your files if you can't create a folder and them move files into it like into it.

As mentioned we solved it by using eloquent for both. This only occurs when combining file with eloquent.

ryanmitchell commented 6 days ago

In my testing it won’t list the folder when you use the database driver for assets no matter what the assets container driver is. This is what I would expect from the code too.

We build the listing of folders from the database. So if there is no asset in that folder it’s ignored.

jymden commented 6 days ago

That is very strange. In our testing it works fine as long as they are the same (as stated above). It works fine in dev locally and on the server. Same behavior no matter what disk we use, local, assets, s3.

But if you do expect this behavior, I do think documenting that it is different from vanilla Statamic. is a good idea. I don't think anyone would expect that the folder should disappear (but still be there on disk) just because the meta data of the containers or assets are stored in the database. This makes it impossible to create a folder just to move existing files. You have to create a folder.

ryanmitchell commented 6 days ago

I've updated the readme.

daun commented 4 days ago

@ryanmitchell Am I understanding correctly that having assets stored in the database will not allow creating an empty folder in the control panel prior to moving existing assets into it?

ryanmitchell commented 3 days ago

No, you can make the folder and then add to it through the CP.

daun commented 3 days ago

@ryanmitchell Okay, sounds promising! I'm a bit confused about what this issue is about then, but will give it a try :)

jymden commented 3 days ago

I don't know why you weren't able to recreate it the same way as we had it. I get the same result in a fresh installed Statamic with eloquent driver. We noticed this when our customer's admin tried to create folders and reported problems.

If you put "file" as driver on asset _containers and "eloquent" on assets (or vice versa). The folders you create disappears from the CP. They are still there on the actual disk, so you can't create them again, under the same name.

But if you use "file" on both or "eloquent" on both you can create folders and they remain in CP. Just like it does if you run Statamic without the eloquent driver. Which I would consider the expected behavior.

So this doesn't occur because you have containers or assets in the database. That works fine. It occurs if you combine file with eloquent.

daun commented 3 days ago

Just tried it out on a vanilla install.

Empty folders work just fine if both assets and asset containers are managed in Eloquent. If either of them are managed on the filesystem, empty folders will indeed just disappear. Not a big deal, but something to keep in mind :)

ryanmitchell commented 3 days ago

I've just run a test on my install, both on S3 and on local file system.

Created a new folder in the CP that didnt previously exist. Refreshed the page Folder was still there

If you want me to take this further I need to see either a failing test, or a repo that shows the issue.

jymden commented 3 days ago

Funny that we're getting different results. I'm not going to swear that it's not me doing something wrong :-).

I'm swamped with a deadline at the moment. But I will try to do a documented test from a clean install next week. It's solved for us (by having it all in Eloquent) but if this is an elusive bug it would be great to figure it out.

Thank you for looking into it!