0

We have the usual way to reference related tables through JOINs using:

$query->condition('foo.entity:type.bar', $value);

But how can this be specified when foo is not a direct field but a complex custom one with more than one columns? Say complex_field has three columns, foo1, foo2 and foo3. In a simple condition, without a join, this works (yes, I could also specify the main property that is used as a default when nothing is specified, this is not important here):

$query->condition('complex_field.foo1', $value);

However, trying to use this field as the reference to another entity fails:

$query->condition('complex_field.foo1.entity:type.bar', $value);

results in Invalid specifier: 'entity'.

For reference, let's consider this field:

public static function schema(FieldStorageDefinitionInterface $field_definition): array {
  return [
    'columns' => [
      'foo1' => [
        'type' => 'int',
        'unsigned' => FALSE,
        'not null' => TRUE,
      ],
      'foo2' => [
        'type' => 'int',
        'unsigned' => FALSE,
        'not null' => TRUE,
      ],
      'foo3' => [
        'type' => 'int',
        'unsigned' => FALSE,
        'not null' => TRUE,
      ],
    ],
    'indexes' => [
      'foo1' => ['foo1'],
      'foo2' => ['foo2'],
      'foo3' => ['foo3'],
    ],
    'foreign keys' => [
      'foo1' => [
        'table' => 'table_foo1',
        'columns' => ['foo1' => 'foo1_id'],
      ],
      'foo2' => [
        'table' => 'table_foo2',
        'columns' => ['foo2' => 'foo2_id'],
      ],
      'foo3' => [
        'table' => 'table_foo3',
        'columns' => ['foo3' => 'foo3_id'],
      ],
    ],
  ];
}
5
  • The core entity reference field type has two properties, target_id and entity. What is foo1 in your second example?
    – 4uk4
    Nov 8, 2023 at 12:05
  • An integer that serves as a foreign key. Basically the same as target_id there. I'll add the schema to the question.
    – Gábor
    Nov 8, 2023 at 13:10
  • By the way, yes, the online discussions mention just this case, file.target_id.entity. Mostly identical to my case, that's why I wonder why it doesn't work for me. People discuss that file.entity should also work because target_id is the main property but, as I mentioned in the question, I'm perfectly happy with specifying the column manually, especially as I have three, and only one could be default, anyway.
    – Gábor
    Nov 8, 2023 at 13:17
  • You can't directly join arbitrary tables or fields in a condition. The join though the .entity notation is tied to entity reference fields, this magic will not work with other field types.
    – Hudri
    Nov 8, 2023 at 13:32
  • OK, I see. Considering that I already have live data, the simplest approach will be a hook_query_TAG_alter() for these cases, I can do whatever JOIN I want then. This isn't a contrib module but an internal need for a single site. I also considered replicating the funcionality of the entity reference but with everything said thrice, it wasn't that attractive a solution... Actually, all other requirements are well served with the field as is, so there's really no need to rework the whole for just this single query.
    – Gábor
    Nov 8, 2023 at 15:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.