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

Search Results - Taxonomy terms returns raw array #326

Closed joshtaylordev closed 1 month ago

joshtaylordev commented 1 month ago

Bug description

When using the search tag with Eloquent Driver, the results return the taxonomy term field as a raw array of slugs. E.g.

"categories" => array:1 [▼
    0 => "category-1"
 ]

Whereas when using flat file it returns an Augmented Array so you can access title, url e.g.

Screenshot 2024-07-18 at 16 20 24

How to reproduce

Create an entry and add a category with the Taxonomy Terms Field. Create a search form or just add the search param to the url e.g. ?q=test Use the search tag to output the results

{{ search:results as="posts" }}
    {{ posts scope="post" }}
        {{ post | dump }}
    {{ posts }}
{{ /search:results }}

Logs

No response

Environment

Environment
Application Name: INDX
Laravel Version: 11.14.0
PHP Version: 8.3.9
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: indx.test
Maintenance Mode: OFF
Timezone: Europe/London
Locale: en

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

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

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

Statamic Addons
ndx/statamic-bard-color-picker: 2.0.0
statamic/eloquent-driver: 4.8.1
studio1902/statamic-peak-browser-appearance: 3.5.0
studio1902/statamic-peak-commands: 8.3.2
studio1902/statamic-peak-seo: 8.14.0
studio1902/statamic-peak-tools: 6.1.0
thoughtco/statamic-cache-tracker: 0.5.0

Statamic Eloquent Driver
Asset Containers: eloquent
Assets: eloquent
Blueprints: file
Collection Trees: eloquent
Collections: eloquent
Entries: eloquent
Forms: eloquent
Global Sets: eloquent
Global Variables: eloquent
Navigation Trees: eloquent
Navigations: eloquent
Revisions: file
Taxonomies: eloquent
Terms: eloquent
Tokens: eloquent

Additional details

I tried to get around the issue by transforming the data following the example from Statamic docs. However, just adding the title example from the docs causes the page to error with: Serialization of 'Closure' is not allowed. It's probably a separate issue but thought I should mention it here

ryanmitchell commented 1 month ago

The serialisation of closure is because youre trying to cache your config - if you use the class based transformer this wont happen.

Can you share your blueprint and collection config - this seems pretty odd - I'm surprised to see any difference in the drivers for this sort of issue.

joshtaylordev commented 1 month ago

I'm a little confused the environment file I shared says Config: NOT CACHED unless I'm missing something?

Below is the collection and blueprint configs for Articles and Article Categories. The Collections are using the eloquent driver and the Blueprints are using flat file.

Article blueprint config:

tabs:
  main:
    display: Main
    sections:
      -
        fields:
          -
            handle: title
            field:
              type: text
              required: true
              validate:
                - required
          -
            handle: excerpt
            field:
              type: textarea
              display: Excerpt
      -
        display: Content
        fields:
          -
            handle: content
            field:
              remove_empty_nodes: false
              type: bard
              display: Content
              buttons:
                - h3
                - h4
                - h5
                - h6
                - small
                - bold
                - underline
                - italic
                - strikethrough
                - color
                - unorderedlist
                - orderedlist
                - removeformat
                - quote
                - anchor
                - image
                - table
  sidebar:
    display: Sidebar
    sections:
      -
        fields:
          -
            handle: slug
            field:
              type: slug
              localizable: true
              validate: 'max:200'
          -
            handle: date
            field:
              type: date
              required: true
              default: now
              validate:
                - required
              display: 'Publish Date'
          -
            handle: article_categories
            field:
              taxonomies:
                - article_categories
              type: terms
              display: Category
title: Article

Article collection config (exported into flat file, this will need to be imported into a DB):

title: Articles
template: articles/show
layout: layout
mount: 20f4966b-dfb6-4fb3-8a9a-ea2a6c897444
taxonomies:
  - article_categories
revisions: false
route: '/{mount}/{slug}'
date: true
sort_dir: asc
date_behavior:
  past: public
  future: private
preview_targets:
  -
    label: Entry
    url: '{permalink}'
    refresh: true

Article category blueprint config:

tabs:
  main:
    display: Main
    sections:
      -
        fields:
          -
            handle: title
            field:
              type: text
              required: true
              validate:
                - required
          -
            handle: content
            field:
              type: markdown
              localizable: true
  sidebar:
    display: Sidebar
    sections:
      -
        fields:
          -
            handle: slug
            field:
              type: slug
              required: true
              validate:
                - required
                - 'max:200'
title: 'Article Category'

Article category collection config (exported into flat file, this will need to be imported into a DB):

title: 'Article Categories'
preview_targets:
  -
    label: Term
    url: '{permalink}'
    refresh: true
layout: layout

Let me know if you need anything else

ryanmitchell commented 1 month ago

Unfortunately I'm not seeing what you are seeing, as no matter whether I use eloquent or file driven I only get an array of handles, not actual terms. Are you able to pull together a repository showing the issue?

joshtaylordev commented 1 month ago

Hi Ryan,

I think I must have been missing the search parameter in the url when I switched back to flat file or something, as I can't seem to replicate the issue at all now. Sorry for wasting your time on that one!

I'm still not quite sure what is happening with the Serialization of 'Closure' is not allowed error when trying to transform the search data as my config shouldn't be cached. But I'm happy to close this issue off, as we've found a work around for now

Thanks