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 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab