doudou8783
2012-10-17 14:27
浏览 67
已采纳

在Doctrine实体上实现自定义字段

I have an Attachment Entity in Doctrine which references a file on Amazon S3. I need to be able to provide a sort of 'Calculated Field' on the Entity that works out what I call the downloadpath. The downloadpath would be a calculated URL, for example http://site.s3.amazon.com/%s/attach/%s where I need to replace the two string values with values on the entity itself (account and filename), so;

http://site.s3.amazon.com/1/attach/test1234.txt

Although we use a Service Layer, I'd like the downloadpath to be available on the Entity at all times without it having to pass through the SL.

I've considered the obvious route of adding say a constant to the Entity;

const DOWNLOAD_PATH = 'http://site.s3.amazon.com/%s/attach/%s'; and a custom getDownloadPath() but I'd like to keep specifics like this URL in my app's configuration, not the Entities class (also, see update below)

Does anyone have any ideas on how I could achieve this?

UPDATE To add to this, I am aware now that I would need to generate a temporary URL with the AmazonS3 library to allow temporary authed access to the file - I'd prefer not to make a static call to our Amazon/Attachment Service to do this as It just doesn't feel right.

图片转代码服务由CSDN问答提供 功能建议

我在Doctrine中有一个 Attachment 实体,它引用了Amazon S3上的文件。 我需要能够在实体上提供一种“计算字段”,它可以解决我所谓的 downloadpath downloadpath 将是一个计算出的URL,例如 http: //site.s3.amazon.com/%s/attach/%s 我需要用实体本身的值(帐户和文件名)替换两个字符串值,所以;

http://site.s3。 amazon.com/1/attach/test1234.txt

虽然我们使用服务层,但我希望 downloadpath 可用于 实体在任何时候都没有必须通过SL。

我认为添加的明显路径是对实体说一个常量; < p> const DOWNLOAD_PATH ='http://site.s3.amazon.com/%s/attach/%s'; 和自定义 getDownloadPath()但是我 d喜欢在我的应用程序配置中保留此URL的详细信息,而不是实体类(另请参阅下面的更新)

有没有人对如何实现这一点有任何想法?

更新要添加到此,我现在知道我需要使用AmazonS3库生成临时URL以允许对文件进行临时的自动访问 - 我是 我不想对我们的亚马逊/附件服务进行静态调用,因为它感觉不对。< / p>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duan198409 2012-10-18 14:55
    已采纳

    Turns out the cleanest way to do this is using the postLoad event like so;

    <?php
    
    namespace My\Listener;
    
    use Doctrine\Common\EventSubscriber;
    use Doctrine\ORM\Events;
    use Doctrine\ORM\Event\LifecycleEventArgs;
    use My\Entity\Attachment as AttachmentEntity;
    use My\Service\Attachment as AttachmentService;
    
    class AttachmentPath implements EventSubscriber
    {
        /**
         * Attachment Service
         * @param \My\Service\Attachment $service
         */
        protected $service;
    
        public function __construct(AttachmentService $service)
        {
            $this->service = $service;
        }
    
        public function getSubscribedEvents()
        {
            return array(Events::postLoad);
        }
    
        public function postLoad(LifecycleEventArgs $args)
        {
            $entity = $args->getEntity();
    
            if ($entity instanceof AttachmentEntity) {
                $entity->setDownloadPath($this->service->getDownloadPath($entity));
            }
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题