Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x]: Related to field not working on custom field handle #15042

Closed
WHITE-developer opened this issue May 21, 2024 · 1 comment
Closed

[5.x]: Related to field not working on custom field handle #15042

WHITE-developer opened this issue May 21, 2024 · 1 comment
Labels

Comments

@WHITE-developer
Copy link

What happened?

Description

When trying to create a relation query and passing in the sourceElement field where the field handle has been changed, the
query won't return the expected result as only the global fields will be checked (https://github.com/craftcms/cms/blob/5.x/src/elements/db/ElementRelationParamParser.php#L528)

Steps to reproduce

  1. Have a related to query where the field is a field with a custom handle
Entry::find()
  ->section('optionGuides')
  ->relatedTo([
    'and',
    [
      'field' => 'gender',
      'sourceElement' => $genderAgeGroup['productGender']->ids(),
    ],
    [
      'field' => 'ageGroup',
      'sourceElement' => $genderAgeGroup['productAgeGroup']->ids(),
    ],
  ])
  ->optionName($optionName)
  ->exists();
  1. query will return false, while it should return true as there is an entry that match these params

when using a simple query:

Entry::find()
  ->section('optionGuides')
  ->relatedTo([
      'and',
      $genderAgeGroup['productGender']->ids(),
      $genderAgeGroup['productAgeGroup']->ids(),
  ])
  ->optionName($optionName)
  ->exists();

It does return true, what is the expected result

Craft CMS version

5.1.4

PHP version

8.3.6

Operating system and version

Ubuntu 24.04

Database type and version

mysql 8.0

Image driver and version

No response

Installed plugins and versions

No response

@i-just
Copy link
Contributor

i-just commented May 22, 2024

Hi, thanks for reaching out! The relatedTo.field element query param needs to be set to the original field’s handle (not the overridden instance handle), and you can still use a field id (and an array). (This change was made in https://github.com/craftcms/cms/releases/tag/5.0.0-beta.8).

We’ll add that to the docs!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants