douwu8524 2014-07-03 21:47
浏览 60
已采纳

向PHP域对象添加方法,将属性转换为RDBMS友好表示

Let's say I have an object in PHP that is used in multiple places in one general representation. This object has several properties that could be either other objects or associative arrays, et cetera.

I am using the Data Mapper pattern to persist these objects, and while this is the first such complex object so far in this application, I'm wondering about the best way to integrate it with data mapper.

I'm wondering if it's a beneficial design decision to implement a public makeDBSafe() method to the object. For instance, the makeDBSafe() method serializes particular properties of the attribute which are too complex for normal SQL data types.

For instance:

$myObject = new MyObject($data);
// Application logic with $myObject
$myObject->makeDBSafe();
$MyObjectMapper->save($myObject);

The way I view it, if the database implementation has to change, then this doesn't affect how I choose to serialize the objects. If I want to change the actual serialized representation (ie JSON/XML whatever) then I would change it in only one place and per class. Also the Data Mapper would not know how the object is being serialized, it will only be provided with a "ready-to-save" object.

  • 写回答

1条回答 默认 最新

  • doudao1922 2014-07-03 22:42
    关注

    If you read Martin Fowler's description of the Data Mapper pattern you would see that a domain object should know nothing about persistence layer. It is the responsibility of the Data Mapper to convert an in-memory object to persistence schema and back.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度