skip to Main Content

I’ve a association in my EntityDefinition:

...
class ParentEntityDefinition extends EntityDefinition
{
    ...
    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            
            (new OneToOneAssociationField(
                'childEntity',
                'id',
                'parent_entity_id',
                ChildEntityDefinition::class,
                false)),
        ]);
    }

    ...
}

The ChildEntity has a fk_field to currency_entity. To avoid an additional request to get the currency, my question:

  • Is, and if, how is it posible to get the currency entity with nested association?

2

Answers


  1. Yes that is totally possible.

    You can add nested associations in the following way:

    $criteria->addAssociation('childEntity.parentEntity');
    

    Note the dot syntax to specify nested associations, the same syntax can be used over the HTTP-API as well.

    Take a look at the official docs as well: https://developer.shopware.com/docs/guides/plugins/plugins/framework/data-handling/reading-data#associations

    Login or Signup to reply.
  2. Yes, just use the dot notation to chain associations when you create the Criteria programmatically. Given you use the repository of the parent entity:

    $criteria = new Criteria();
    $criteria->addAssociation('childEntity.currencyEntity');
    

    In the request body for an API search request:

    {
        "associations": {
            "childEntity": {
                "associations": {
                    "currencyEntity": {}
                }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search