API-平台 - 通过 IRI 迭代资源

API-Platform - Iterating Resource Via IRI

在我的 Symfony 应用程序中,我有一个订阅者,我需要在其中迭代存储在数组中的 IRI 并访问其实体的方法。我该怎么做?

例如:

function sendMail ($event) {
    ...
    $instance = $event->getControllerResult();
    ...
    $recipients = $instance->getRecipients(); // returns array of IRIs
    foreach ($recipients as $recipient) {
        $r = // instance of IRI-associated entity
        if ($r instanceof User) {
            // send to user
            $email = $r->getEmail();
            // send an email
        } else if ($r instanceof Group) {
            // send to group
            foreach ($r->getUsers() as $user) {
                $email = $user->getEmail();
                // send an email
            }
        }
    }
    ...
}

虽然我可能忽略了它,但我没有在文档中找到这样做的方法,而且我对 Symfony 的了解仍在增长。

您可以尝试将 IriConverterInterface $iriConverter 传递给 __constructor。并将您的 Iri 转换为一个实体,例如:

private $iriConverter;

public function __construct(IriConverterInterface $iriConverter)
{
    $this->iriConverter = $iriConverter;
}

public function sendMail ($event) {
   foreach ($recipients as $recipientIri) {
        $recipient = $iriConverter->getItemFromIri($recipientIri)
        if ($recipient instanceof User) {
            $email = $recipient->getEmail();
            ...
        } 
    ...
    }
}