craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.28k stars 637 forks source link

[5.x]: Entry Query returns unexpected results post content refactor #16149

Closed CallumK-Enigma closed 6 days ago

CallumK-Enigma commented 6 days ago

What happened?

Description

After upgrading from Craft CMS 4.5.11.1 to 5.5.0.1 I have observed some weird behaviour with the craft.entries() twig query. The query appears to return a number of entries, some full entries, others with no data other than an ID. Checking those ID's in the entries table, the primaryOwnerId links to a record in the elements table that is an old revision.

This has occurred off of the back of a separate issue which I found had been addressed [https://github.com/craftcms/cms/issues/16032] when updating the Craft CMS version.

Steps to reproduce

  1. Update Craft CMS from 4.5.11.1 to 5.5.0.1
  2. Create a twig entry query which will fetch all the results. One used for this issue is found below {% set entries = craft.entries().search(searchQuery).orderBy('score').all() %}
  3. view query results

Expected behavior

Query will return an array of fully populated Entry objects relevant to the query

Actual behavior

Query returns an array of Entry objects, some of which are fully populated with the expected data. Others have nothing but an ID, and appear to tie to an old revision on an entry through the the primaryOwnerId link to the entries table.

Craft CMS version

5.5.0.1

PHP version

8.2.22

Operating system and version

Linux 5.15.153.1-microsoft-standard-WSL2

Database type and version

MariaDB 10.6.18

Image driver and version

GD 8.2.22

Installed plugins and versions

i-just commented 6 days ago

Hi, thanks for reaching out! I haven’t been able to replicate the revisions being returned when using .search(). However, please note that nested entries are also returned by default when you query entries in Craft 5. There’s more info on that here: https://craftcms.com/docs/5.x/system/searching.html#searching-nested-elements.

Can you first try changing your query to only search through entries that belong to e.g. any section (craft.entries().section('*').search(searchQuery).orderBy('score').all()) and see if that returns the desired results?

CallumK-Enigma commented 6 days ago

Hi, @i-just

Looks like the suggested was exactly the issue! Resolved now!

i-just commented 6 days ago

Great news! Glad to hear it’s all working as expected.