Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use different Eloquent models for different collections #21

Open
slackernrrd opened this issue May 28, 2021 · 6 comments
Open

Use different Eloquent models for different collections #21

slackernrrd opened this issue May 28, 2021 · 6 comments
Labels
wontfix This will not be worked on

Comments

@slackernrrd
Copy link

I would really like to use Statamic in a project with a traditional datbase layout, but it looks like it's impossible or very difficult.

I have tried following the code in this repository (and the guide in the knowledge base) to implement this myself. It seemed promising since Statamic uses a repository, but currently this driver (and Statamic in general) assumes that all entries, in any collection, are stored in a single database table. This approach works when using a JSON data column, but fails when trying to apply it to a more traditional database layout where different kinds of content are stored in different tables.

Also when trying to see if I could somehow get around this limitation by making a clever QueryBuilder implementation I noticed that many of the interfaces (like Statamic\Contracts\Entries\QueryBuilder) are empty, so it's difficult to know what methods need to be implemented.

Anyway, is this feature even possible to implement?

@jesseleite jesseleite added the feature request New feature or request label Sep 29, 2021
@jesseleite
Copy link
Member

Great feature request. Maybe a duplicate of #11 though?

@slackernrrd
Copy link
Author

They are similar, but the feature discussed in #11 would still use the same Eloquent model/database table for the selected collections, whereas what I am proposing here is that it would also be possible to specify a Model per collection.

@jesseleite
Copy link
Member

Fair enough, will leave open.

@ryanmitchell
Copy link
Contributor

Is this not exactly what the Runway add-on lets you do? https://statamic.com/addons/double-three-digital/runway

@MtDalPizzol
Copy link

Hey folks. I'm also REALLY needing this.

In my case, all I need is to have each collection in its own table. There's no need for each field to be in a separate column.

So, my idea was this:

When a new collection is created, we only need to create the table to hold the entries for that collection using the same structure, what can even be done at runtime using Laravel's Schema Builder. This way, we can have each collection in a separate table, but still have all the benefits and flexibility of Statamic's blueprints without having to create migrations every time ou change our blueprint structure, since every table will have the same structure and data coming from the form will still be stored at a data column, just like it's already done in the entries table.

Then, I thought to simply use the same EntryModel for every table, but set the table for the model at runtime using the collection handle and eloquent's setTable method. This will prevent us from having to create a specific model for each table every time we create a new collection.

At first I thought I could simply tweek the EntryRepository and the EntryQueryBuilder. However, it turned into a 2 day code digging nightmare where I couldn't find where the EntryQueryBuilder sets the table it uses and where to set the table for the models was even more challenging and unsuccessful.

High heavens frustration levels.

Does anyone have some sort of direction to this?

@ryanmitchell ryanmitchell added wontfix This will not be worked on and removed feature request New feature or request labels Jun 26, 2023
@ryanmitchell
Copy link
Contributor

For this use case I highly recommend using Runway

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants