【问题标题】:API-Platform - Iterating Resource Via IRIAPI 平台 - 通过 IRI 迭代资源
【发布时间】:2020-08-06 04:27:46
【问题描述】:

在我的 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 的了解仍在增长。

【问题讨论】:

    标签: php symfony4 api-platform.com


    【解决方案1】:

    您可以尝试将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();
                ...
            } 
        ...
        }
    }
    
    

    【讨论】:

    • 轰隆隆!谢谢你。这正是我一直在寻找的。它完美地做到了。
    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 2018-03-26
    • 2013-07-03
    • 1970-01-01
    • 2019-08-17
    相关资源
    最近更新 更多