用php/Symfony修饰实体

用php/Symfony修饰实体,php,symfony,Php,Symfony,我知道如何在symfony()中装饰服务 有没有办法在symfony中装饰实体 我有实体发票,想为它们设置绝对url。 绝对url基于parameters.yml和路由器(注入)。 我想为所有实体类型发票,有绝对的url。 我在谷歌上搜索过,但找不到路。 顺便说一句 我知道我可以使用服务,但问题就在标题中 “有没有办法在symfony中装饰实体?” 示例代码(伪代码多于工作代码): 您可以使用注入加载的内容。谢谢Yoshi。这就是我要找的。我将使用postLoad事件。请您将您的决议作为anws

我知道如何在symfony()中装饰服务

有没有办法在symfony中装饰实体

我有实体发票,想为它们设置绝对url。 绝对url基于parameters.yml和路由器(注入)。 我想为所有实体类型发票,有绝对的url。 我在谷歌上搜索过,但找不到路。 顺便说一句

我知道我可以使用服务,但问题就在标题中 “有没有办法在symfony中装饰实体?”

示例代码(伪代码多于工作代码):


您可以使用注入加载的内容。谢谢Yoshi。这就是我要找的。我将使用postLoad事件。请您将您的决议作为anwser发布好吗?
<?php

namespace AppBundle\Service\Invoices\Decorators;

use AppBundle\Entity\Invoices;
use Symfony\Component\Routing\Router;

class InvoiceAbsoluteUrlDecorator
{
    /**
     * @var string - injected from symfony parameters.yml
     */
    private $absoluteUrl;

    /** @var  Router */
    private $router;

    /** @var  Invoices */
    private $invoice;

    public function __construct($invoice, $absoluterUrl, $router)
    {
        $this->invoice = $invoice;
        $this->absoluteUrl = $absoluterUrl;
        $this->router = $router;

        $this->setInvoiceUrl($invoice);
    }

    public function __call($method, $args)
    {
        if (!method_exists($this->invoice, $method)) {
            throw new Exception("Undefined method $method attempt in the Url class here.");
        }
        return call_user_func_array(array($this->invoice, $method), $args);
    }

    private function setInvoiceUrl($invoice)
    {
        $this->invoice->setInvoiceFileUrl(
            $this->absoluteUrl
            . $this->router->generate(
                'download_file',
                [
                    'id' => $invoice->getInvoiceFile()->getId(),
                    'byEntityId' => 0
                ]
            )
        );
    }
}