eloquent-driver icon indicating copy to clipboard operation
eloquent-driver copied to clipboard

Support mapping of entry data to database columns

Open ryanmitchell opened this issue 1 year ago • 2 comments

This is a WIP.

This PR makes it possible to store the contents of data handles in alternative columns in your database, eg you may want to store my_key in a column called my_column.

The benefit of this is it allows you to index that column separately which can speed up database operations.

Mapping can be added by adding a hook on the eloquent entry class, and updating the array $payload. This array is a mapping of data handles to database columns (note that only root-level data keys are supported).

        Entry::hook('data-column-mappings', function (array $payload, $next) {
            $payload = array_merge($payload, ['my_key' => 'my_column']);

			return $next($payload);
        });

Closes https://github.com/statamic/eloquent-driver/issues/272

ryanmitchell avatar Apr 15 '24 07:04 ryanmitchell

@duncanmcclean @jasonvarga would appreciate your thoughts on this when you get a chance.

ryanmitchell avatar Apr 15 '24 08:04 ryanmitchell

The statamic-5 branch has been merged, so this PR should target master.

jasonvarga avatar May 03 '24 14:05 jasonvarga

Mapping can be added by adding a hook on the eloquent entry class, and updating the array $payload. This array is a mapping of data handles to database columns (note that only root-level data keys are supported).

Just thinking out loud here.... instead of mapping the columns using a hook, is there any way the Eloquent Driver could figure out those columns itself, based on the columns in the database?

Maybe it could get a list of the model's columns, filters out columns that already get mapped (like slug, data, updated_at, etc), then use the columns that are left to read/write fields from?

duncanmcclean avatar Jun 07 '24 15:06 duncanmcclean

It could, but that would mean an extra query every time to get the columns. Not a hugely expensive operation but worth considering. It also limits us to root keys only - the current mapping approach could quite easily be extended to allow non-root keys.

ryanmitchell avatar Jun 09 '24 06:06 ryanmitchell