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 get data returning from API #288

Closed 1stevengrant closed 1 month ago

1stevengrant commented 1 month ago

Have the eloquent driver working in terms of being able to manage and see content in the CP.

I did have to update the entries array to 'model' => \Statamic\Eloquent\Entries\UuidEntryModel::class, in order to create without an error.

All well and good.

However, when I attempt to hit our content API /content-api/collections/events/entries the data array is empty.

I thought this might be due to the collections using the file driver and entries using the eloquent driver but even having both set to eloquent, yields the same result.

If I change both to file then the API behaves as expected.

Here's the API config

<?php

return [

    'enabled' => true,

    'resources' => [
        'collections' => true,
        'navs' => true,
        'taxonomies' => true,
        'assets' => true,
        'globals' => true,
        'forms' => true,
        'users' => true,
    ],

    'route' => 'content-api',

    'middleware' => env('STATAMIC_API_MIDDLEWARE', 'api'),

    'pagination_size' => 50

    'cache' => [
        'expiry' => 60,
    ],

    'excluded_keys' => [
        //
    ],

];

and here's the eloquent driver config

<?php

return [

    'connection' => env('STATAMIC_ELOQUENT_CONNECTION', ''),
    'table_prefix' => env('STATAMIC_ELOQUENT_PREFIX', ''),

    'asset_containers' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Assets\AssetContainerModel::class,
    ],

    'assets' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Assets\AssetModel::class,
        'asset' => \Statamic\Eloquent\Assets\Asset::class,
    ],

    'blueprints' => [
        'driver' => 'file',
        'blueprint_model' => \Statamic\Eloquent\Fields\BlueprintModel::class,
        'fieldset_model' => \Statamic\Eloquent\Fields\FieldsetModel::class,
    ],

    'collections' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Collections\CollectionModel::class,
        'update_entry_order_queue' => 'default',
        'update_entry_order_connection' => 'default',
    ],

    'collection_trees' => [
        'driver' => 'eloquent',
        'model' => \Statamic\Eloquent\Structures\TreeModel::class,
        'tree' => \Statamic\Eloquent\Structures\CollectionTree::class,
    ],

    'entries' => [
        'driver' => 'eloquent',
        'model' => \Statamic\Eloquent\Entries\UuidEntryModel::class,
        'entry' => \Statamic\Eloquent\Entries\Entry::class,
    ],

    'forms' => [
        'driver' => 'file',
        'model'  => \Statamic\Eloquent\Forms\FormModel::class,
    ],

    'form_submissions' => [
        'driver' => 'eloquent',
        'model'  => \Statamic\Eloquent\Forms\SubmissionModel::class,
    ],

    'global_sets' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Globals\GlobalSetModel::class,
    ],

    'global_set_variables' => [
        'driver' => 'eloquent',
        'model' => \Statamic\Eloquent\Globals\VariablesModel::class,
    ],

    'navigations' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Structures\NavModel::class,
    ],

    'navigation_trees' => [
        'driver' => 'eloquent',
        'model' => \Statamic\Eloquent\Structures\TreeModel::class,
        'tree' => \Statamic\Eloquent\Structures\NavTree::class,
    ],

    'revisions' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Revisions\RevisionModel::class,
    ],

    'taxonomies' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Taxonomies\TaxonomyModel::class,
    ],

    'terms' => [
        'driver' => 'eloquent',
        'model' => \Statamic\Eloquent\Taxonomies\TermModel::class,
    ],

    'tokens' => [
        'driver' => 'file',
        'model' => \Statamic\Eloquent\Tokens\TokenModel::class,
    ],
];
duncanmcclean commented 1 month ago

What's the output of php please support:details?

1stevengrant commented 1 month ago
Environment
Application Name: Local Platform
Laravel Version: 10.48.9
PHP Version: 8.3.4
Composer Version: 2.7.6
Environment: local
Debug Mode: ENABLED
URL: local.test
Maintenance Mode: OFF

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

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

Sentry
Enabled: MISSING DSN
Environment: local
Laravel SDK Version: 3.8.2
PHP SDK Version: 3.22.1
Release: NOT SET
Sample Rate Errors: 100%
Sample Rate Performance Monitoring: NOT SET
Sample Rate Profiling: NOT SET
Send Default PII: DISABLED

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

Statamic Addons
statamic/eloquent-driver: 4.0.0

Statamic Eloquent Driver
Asset Containers: file
Assets: file
Blueprints: file
Collection Trees: eloquent
Collections: file
Entries: eloquent
Forms: file
Global Sets: file
Global Variables: eloquent
Navigation Trees: eloquent
Navigations: file
Revisions: file
Taxonomies: file
Terms: eloquent
1stevengrant commented 1 month ago

API endpoint for a taxonomy works swell.

ryanmitchell commented 1 month ago

Before I go digging into it is there any chance you've got a cached api response? It defaults to caching for 60 minutes, so if you clear the artisan cache it should give you a fresh response.

1stevengrant commented 1 month ago

Thanks Ryan, should have said that I already cleared caches and issue still persisted.

ryanmitchell commented 1 month ago

I'm not able to see any reason for this not to be working to be honest - I've tested a couple of v5 installs and they seem to work fine. Is it possible to get access to this site, or for you to give Statamic support access?

edmundayris commented 1 month ago

Hi,

I'm running into the same issue.

One thing I've noticed is that collections that don't use the publish dates option don't return any data, while my collections that do use the publish dates option seem to work fine.

Digging in a bit I can see it generates the following query with a date condition, but the date for my entries is null since it doesn't use the publish date option.

select * from `entries` where `collection` = ? and `published` = ? and ((`collection` = ? and `date` < ?))

Maybe that helps a little?

ryanmitchell commented 1 month ago

@edmundayris thats really helpful, thank you. If you're right, then this PR (or whatever replaces it) should help

jasonvarga commented 1 month ago

I can't reproduce this. Could someone please provide us with a site that has this issue?

1stevengrant commented 1 month ago

Sure @jasonvarga - I'll get a repo up.

1stevengrant commented 1 month ago

here's an example repo with sqlite db

https://github.com/1stevengrant/statamicsandpit

1stevengrant commented 1 month ago

Hi,

I'm running into the same issue.

One thing I've noticed is that collections that don't use the publish dates option don't return any data, while my collections that do use the publish dates option seem to work fine.

Digging in a bit I can see it generates the following query with a date condition, but the date for my entries is null since it doesn't use the publish date option.

select * from `entries` where `collection` = ? and `published` = ? and ((`collection` = ? and `date` < ?))

Maybe that helps a little?

can confirm this is the case. If I enable date publishing on the collections, they return fine

jasonvarga commented 1 month ago

I think you can workaround it by removing these lines:

https://github.com/1stevengrant/statamicsandpit/blob/b5ec0446d5555b46ef4a7f7fd30e3b3117a7447f/content/collections/books.yaml#L3-L5

(Or making them both public)