Skip to content

Conversation

@ryanmitchell
Copy link
Contributor

@ryanmitchell ryanmitchell commented Apr 15, 2024

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_key.

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).

The mapping 'auto-magically' happens by checking if a column in your entries table exists with the same name as your field handle, and where it finds it, it saves the field value there instead of the data column.

Following some discussion with Duncan, some additional thoughts:

  1. If you are adding a migration to add a new column your schema based on an existing blueprint column, you'll need to save the entries to repopulate them Entry::all()->each->save(); as part of your migration.

  2. We're going to wrap this in a config to avoid it being a breaking change, so to enable this feature you'll need to set statamic.eloquent-driver.entries.map_data_to_columns to be true

Closes #272

ryanmitchell and others added 25 commits June 23, 2023 16:40
…ory and Query Builder (statamic#177)

* Add upgrade note to readme

* Begin split configs

* Use form handle to relate, rather then a database id

* Defer to core methods for retrieving submissions

* Update fresh migrations and add migration for existing installs

* Eloquent FormRepository

* Fix migration

* Dont force a model type or it can't be overwritten

* Missing update script

* Correct file path to migration

* Lets not exclude parent

* StyleCI

* Actually run update script

* Prep for Statamic 5

* Update dev dependencies

* Try again

* Meh

* Remove deprecated test functions

* static

* Migrate PHP unit config

* Fix migration

* Fix tests to use handle instead of id

* Bug fixes

* Fix form listing bugs
* Split migration tags

* fix everything

* Merge branch '5.x-support' into split-migration-tags

* Ignore `.phpunit.cache`

---------

Co-authored-by: Ryan Mitchell <ryan@thoughtcollective.com>
* Simplify `TestCase`

* Remove `partialMock` method from `TestCase`

I'm not sure which "earlier versions of Laravel" this is referring to but the test suite seems to pass without it so I presume it's no longer needed.

* doesn't look like we're using this method either

* The `ConsoleKernel` isn't needed
* Eloquent driver should be opt-in, not opt-out

* Swap all drivers to `eloquent` in our `TestCase`
* Fix asset import bug

* Drop status on entries

* Revert "Fix asset import bug"

This reverts commit 35ebe65.

* Fix test

* StyleCI

* Revert

* Proper update script

* Make status nullable in down migration
* Initial docs refresh

* Fix tpyo

* Missing space

---------

Co-authored-by: Ryan Mitchell <ryan@thoughtcollective.com>
@ryanmitchell ryanmitchell changed the title Add upgrade note to readme Support mapping of entry data to database columns Apr 15, 2024
@ryanmitchell
Copy link
Contributor Author

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

duncanmcclean and others added 3 commits April 17, 2024 10:16
* Fix failing tests in `EntryQueryBuilderTest`

* dont need to freeze time for this test
@jasonvarga
Copy link
Member

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

@ryanmitchell ryanmitchell changed the base branch from statamic-5 to master May 3, 2024 14:03
@duncanmcclean
Copy link
Member

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?

@ryanmitchell
Copy link
Contributor Author

ryanmitchell commented Jun 9, 2024

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 ryanmitchell marked this pull request as ready for review June 21, 2024 06:33
@ryanmitchell ryanmitchell merged commit 7fd6c23 into statamic:master Jul 1, 2024
@ryanmitchell ryanmitchell deleted the feature/mapped-data-columns branch July 1, 2024 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make it possible to save to specific columns instead of data

3 participants