通过 ID 获取 Shopware 6 实体的推荐方法是什么?

Ale*_*lex 2 data-access-layer shopware

我有一个实体 ID,想通过存储库获取实体对象,目前我们这样做

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

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

    if($property) {
        return $property->get('groupId');
    }
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法,比如 Laravel 的find方法?

Sth*_*ing 5

正如您在自己的答案中已经提到的,您可以将 ID 作为参数传递给 Criteria 对象。

我建议$property = $this->propertyGroupOptionRepository->search(new Criteria([$propertyId]), $context)->first()

find 方法未实现,我认为永远不会实现,因为您可能想要加载使用一些关联和/或聚合。