Skip to content

[5.x]: Selecting a slug in an entries query for an entries field returns no results #15414

@MoritzLost

Description

@MoritzLost

What happened?

Description

I just encountered a bug with an entry query that worked in Craft 4, but is broken in Craft 5:

$categories = $job->jobCategories->select('slug')->column();

$job is an entry and jobCategories is an Entries field. This returned an array of slugs (strings) in Craft 4, but in Craft 5 it's always returning an empty array, regardless of how many entries are selected in the field.

Using ->all() instead of ->column shows the same behaviour. In Craft 4 this returns an array of entries (with all other properties and fields empty, of course). In Craft 5, this still returns an empty array.

I also tried selecting some other fields – id works, but title doesn't. I also tried to explicitly select the table and column (elements_sites.slug), but that isn't working either.

Not sure if I'm doing something wrong here. If the select statement was just wrong, I would get an error, e.g. select('foobar') throws an error.

Notably, if I use a plain entries query without starting from an Entries field, the query works as expected. This returns an array of entry slugs:

Entry::find()->limit(5)->select('slug')->column();

So it might be related to the parameters set by relations fields?

Steps to reproduce

  1. Add an Entries field to an entry type's field layout.
  2. Create a new entry and select some entries in the Entries field.
  3. In the template for the entry, access the Entries field and select the slug using the query above.

Craft CMS version

5.2.8

PHP version

8.3

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions