shopwaredbal

How to search through a translated entity in shopware 6?


We want to search for the translated name in Shopware 6 programatically and came up with this:

$criteria = (new Criteria())
   ->addAssociation('property_group_option_translation.name')
   ->addFilter(new EqualsFilter('name', $value))
   ->addFilter(new EqualsFilter('id', $propertyGroupId));

return $this->propertyGroupOptionRepository->search($criteria, Context::createDefaultContext())->getEntities()->first();

But we get

Shopware\Core\Framework\DataAbstractionLayer\Dbal\Exception\UnmappedFieldException : Field "name" in entity "property_group_option" was not found.

How to search for a translated entity in general and in this specific case?


Solution

  • EDIT:

    This should be working

    $criteria = (new Criteria())
        ->addAssociation('property_group_option_translation')
        ->addFilter(new EqualsFilter('property_group_id', $propertyGroupId))
        ->addFilter(new EqualsFilter('name', $value));
    

    Wrong old answer

    This is not working:

    $criteria = (new Criteria())
        ->addFilter(new EqualsFilter('property_group_id', $propertyGroupId))
        ->getAssociation('property_group_option_translation')
           ->addFilter(new EqualsFilter('name', $value));
    

    See also

    https://stackoverflow.com/a/67649705/288568