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 71 forks source link

Unable to order select options in a blueprint when using eloquent driver #312

Open Baumi-Dev opened 6 days ago

Baumi-Dev commented 6 days ago

Bug description

When using eloquent driver, the form input options are stored as JSON data, which doesn't allow for specific ordering. Options should be stored in an array to allow for specific ordering

How to reproduce

Ordering options in flat files works fine, but when using eloquent driver, options are stored in a JSON object, causing the options to be stored in ascending order. Reordering options in the control panel doesn't work, as the options are always stored in ascending order

Logs

No response

Environment

Environment
Application Name: Statamic
Laravel Version: 11.10.0
PHP Version: 8.2.10
Composer Version: 2.7.2
Environment: local
Debug Mode: ENABLED
URL: empower-eats.public.test
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: file
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Statamic
Addons: 1
Sites: 1
Stache Watcher: Enabled
Static Caching: Disabled
Version: 5.11.0 PRO

Statamic Addons
statamic/eloquent-driver: 4.5.0

Statamic Eloquent Driver
Asset Containers: eloquent
Assets: eloquent
Blueprints: eloquent
Collection Trees: eloquent
Collections: eloquent
Entries: eloquent
Forms: eloquent
Global Sets: eloquent
Global Variables: eloquent
Navigation Trees: eloquent
Navigations: eloquent
Revisions: file
Taxonomies: eloquent
Terms: eloquent
Tokens: eloquent

Additional details

No response

ryanmitchell commented 5 days ago

Can you provide a screencast or screenshot of what you are seeing and also the underlying data please?

Baumi-Dev commented 5 days ago

Hi Ryan,

Thanks for getting back to me so quickly.

Please find screen recording and screenshot attached.

Cheers,

Alex Screenshot 2024-06-27 at 3 27 07 PM

Baumi-Dev commented 5 days ago

Here's a screencast as well...

https://github.com/statamic/eloquent-driver/assets/138436068/eeae327b-3fde-4d80-a626-9df3e050f90b

ryanmitchell commented 5 days ago

Thanks - just for clarity it's blueprint related rather than form related.

The underlying database engine reorders the JSON for the field type on save, so we would need to work around that and do something similar to how we handle sections: https://github.com/statamic/eloquent-driver/blob/8172d109b20f19ba9bb71e98210b07c977511a61/src/Fields/BlueprintRepository.php#L201

Baumi-Dev commented 5 days ago

Gotcha! Sorry for the confusion, appreciate your help

ryanmitchell commented 5 days ago

I'll loop round to it when I can, in the mean time if you are able to make the keys numeric it should maintain your ordering. I'll also happily review a PR if its something you'd be prepared to have a go at yourself.

Baumi-Dev commented 5 days ago

Sounds good, thanks! Might have a crack at it this weekend if you haven't already gotten to it before me. Cheers mate

ryanmitchell commented 5 days ago

Great, if you get to it before me I would suggest you extend the DataReferenceUpdater in core to recurse over the fields, as it will save quite a bit of logic creation. I appreciate your use case is form blueprints, but the solution will need to be able to handle selects inside replications (inside replicators....).