doushao1948 2014-01-20 00:25
浏览 60
已采纳

数据映射器模式,文件上载和单一责任原则与Doctrine 2

For a long time I have been using ActiveRecord-like approach for database entities (static classes retrieve rows from DB and instantiate the entity). I know it violates the single responsibility principle, but it somewhat seems more human to me. After reading a lot of articles how bad the AR is, I am trying to "get a data mapper mindset", but I am failing.

I am experimenting with Doctrine 2 in Symfony. I understand the separation of Repositories (they retrieve data from db) and Entities (they hold the data). What I don't know is, where am I supposed to place the process of saving uploaded file.

Let's say I have User entity and every user can save his photo. This photo's path should be stored in the entity, that's clear. But where do I define the path? And what object should save the uploaded photo to the target path?

I have seen the Symfony docs on this topic. They generate the URL within Entity object, but isn't this also violating the single responsibility principle? Why does entity (which only holds data) generate a path on filesystem (even relative)?

This bothers me a lot. I can't get the right mindset to be able to see the advantages of DM as it brings to me a lot of complications.

  • 写回答

2条回答 默认 最新

  • dongpo1599 2014-01-20 00:36
    关注

    You're correct - the example in the symfony docs violates the SR principle.

    A cleaner way of handling uploads would be creating a doctrine listener/subscriber that is responsible for handling this process outside of your entity.

    A very nice implementation is the VichUploaderBundle.

    It can be configured using annotations or YAML ...

    ... provides (optional) integration with the filesystem abstraction library Gaufrette by KnpLabs ...

    ... and is pretty easy to set up.

    I definitely recommend giving it a try or browse the source if you're looking for an implementation example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码