TablesInterface::addField() doesn't document that $field can contain relationships

Created on 2 July 2024, 2 months ago
Updated 20 July 2024, about 2 months ago

Problem/Motivation

The docs say:

   * @param string $field
   *   If it doesn't contain a dot, then an entity base field name. If it
   *   contains a dot, then either field name dot field column or field name dot
   *   delta dot field column. Delta can be a numeric value or a "%delta" for
   *   any value.

But you can chain through relationships, as seen in the test EntityQueryRelationshipTest:

      ->condition("user_id.entity.name", $this->accounts[0]->getAccountName(), '<>')

      ->condition("user_id.entity:user.name", $this->accounts[0]->getAccountName())

Steps to reproduce

Proposed resolution

Rather than document this fully here, we should refer to the comprehensive docs at Drupal\Core\Entity\Query\QueryInterface::condition()

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Documentationย  โ†’

Last updated 1 minute ago

No maintainer
Created by

๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @joachim
  • First commit to issue fork.
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia nishtha.pradhan

    nishtha.pradhan โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Failed
    2 months ago
    Total: 181s
    #218809
  • Pipeline finished with Canceled
    2 months ago
    Total: 295s
    #218820
  • Pipeline finished with Success
    2 months ago
    Total: 513s
    #218826
  • Status changed to Needs review 2 months ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia VinmayiSwamy

    Thank you @nishtha.pradhan, for your MR and for addressing the documentation for TablesInterface::addField() in Drupal core.

    I've reviewed the changes provided in Drupal version 11.x. To enhance clarity, I suggest explicitly stating in the documentation that dot notation (.) can be used to specify fields involving relationships. This is consistent with practices observed in tests such as EntityQueryRelationshipTest.

    For example:

    /**
     * Adds a field to a database query.
     *
     * @param string $field
     *   The field to add to the query. Use dot notation to specify fields from
     *   related entities, e.g., "entity.field" or "entity:referenced_entity.field".
     *   Delta can be a numeric value or a "%delta" for any value.
     *   
     *   See {@link https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Query!QueryInterface.php/function/QueryInterface%3A%3Acondition/8.9.x QueryInterface::condition()} for more details.
     * @param string $type
     *   Join type, can either be INNER or LEFT.
     * @param string $langcode
     *   The language code to use for multilingual queries.
     *
     * @return string
     *   The return value is a string containing the alias of the table, a dot
     *   and the appropriate SQL column as passed in. This allows the direct use
     *   of this in a query for a condition or sort.
     *
     * @throws \Drupal\Core\Entity\Query\QueryException
     *   If $field specifies an invalid relationship.
     */
    public function addField($field, $type = 'INNER', $langcode = NULL);
    

    I would appreciate feedback from others on this proposed change. Does this approach align with the understanding of how fields should be specified in Drupal queries? Are there any additional considerations we should take into account?

    Your thoughts and suggestions would be valuable.

    Thank you!

  • Status changed to Needs work about 2 months ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Seems the updates were slightly copied and paste from the summary, but don't think we should be introducing new phrase that don't already exist.

    For comprehensive documentation on the

    is not found in core. Example

    * See the @link themeable Default theme implementations topic @endlink for
    * details.

    That's just a suggestion, that may fit but should checkout other examples in the repo so we can stay more inline.

    Nitpicky but good exercise, leaving the novice tag.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom joachim

    The @link syntax is for linking a topic. I'm not sure it'll work with a method name.

    Just giving the full class name and method will make a link on api.d.org.

Production build 0.71.5 2024