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

Different object types returned with get() vs other retrieval methods, chunk, lazy #212

Closed Smef closed 4 months ago

Smef commented 9 months ago

Queries return different object types when using get() vs other retrieval methods, such as chunk, chunkById, lazy, or others.

Using limit(xx)->get() returns a Statamic\Eloquent\Entries\Entry object Using chunk() provides a Statamic\Eloquent\Entries\EntryModel object to the callback function

These two objects behave differently, and getting different objects back is an unexpected result. In particular, EntryModel objects don't seem to go through the entry populating process that Entry objects do. This results in things like computed properties not loading.

It would be great to have these different query types return the same object type. Alternatively, being able to query the database using a regular model query and get models back including all the entry functionality of an Entry would be fantastic, which I think would mean expanding the functionality of the EntryModel to be more similar to the Entry object.

ryanmitchell commented 9 months ago

Queries return different object types when using get() vs other retrieval methods, such as chunk, chunkById, lazy, or others.

The core query builder that this package inherits from (https://github.com/statamic/cms/blob/4.x/src/Query/EloquentQueryBuilder.php) has only implemented the methods used by the core package, or that other developers have required and added PRs for.

It would be great to have these different query types return the same object type.

It should be a case of adding those methods to the core query builder, similar to get(): https://github.com/statamic/cms/blob/7156abf4979414387c79ebbf318f87ca5171db9a/src/Query/EloquentQueryBuilder.php#L53-L66

Alternatively, being able to query the database using a regular model query and get models back including all the entry functionality of an Entry would be fantastic, which I think would mean expanding the functionality of the EntryModel to be more similar to the Entry object

This approach wont happen as it goes against the repository pattern.

Also for what its worth, you can query using the EntryModel using any eloquent builder methods, then simply call \Statamic\Eloquent\Entries\Entry::fromModel on any of the resulting models to turn it into an Entry class. Maybe an alternative way of approaching it which gives you the best of both worlds.

Smef commented 9 months ago

This all sounds good. Thanks! fromModel will definitely be something we use in the future.

ryanmitchell commented 4 months ago

chunk() and lazy() are now supported in core, so I'm going close this off.