Search code examples
data-access-layershopware

What is the recommended way to get a Shopware 6 entity by ID?


I have a entity ID and want to get the Entity object via repository, currently we do

$criteria = new Criteria();
    $criteria->addFilter(new EqualsFilter('id', $propertyId));

    $property = $this->propertyGroupOptionRepository->search(
        $criteria,
        $this->context
    )->first();

    if($property) {
        return $property->get('groupId');
    }

Is there a nicer way, like Laravel's find method?


Solution

  • As you already mentioned in your own answer you can pass the IDs as a parameter to the Criteria object.

    I suggest $property = $this->propertyGroupOptionRepository->search(new Criteria([$propertyId]), $context)->first()

    A find method isn't implemented and I assume never will, cause you may want to load use some associations and/or aggragations.