https://culttt.com/2014/06/18/whats-difference-active-record-data-mapper/
什么是ORM
Object Relational Mapper市咽,數(shù)據(jù)庫(kù)和應(yīng)用中間的一層许蓖。面向?qū)ο蟪绦虿僮鞯氖菍?duì)象鹏倘,而像MySQL等數(shù)據(jù)庫(kù)存儲(chǔ)為實(shí)體-關(guān)系狸捅,對(duì)象和實(shí)體關(guān)系間就需要相互轉(zhuǎn)換一姿,ORM所做的就是這件事厨喂。
active record和data mapper
ORM最流行的實(shí)現(xiàn)模式有兩種吴超,active record和data mapper见芹。
active record示例代碼:
$user = new User;
$user->username = ‘philipbrown’;
$user->save();
對(duì)象映射為數(shù)據(jù)庫(kù)的一行。
對(duì)象屬性和數(shù)據(jù)庫(kù)字段按照database schema映射蠢涝。
data mapper示例代碼:
$user = new User;
$user->username = ‘philipbrown’;
EntityManager::persist($user);
領(lǐng)域?qū)ο蟛恍枰浪鼈冊(cè)跀?shù)據(jù)庫(kù)中是怎么存的辆童,
二者的選擇
沒(méi)有誰(shuí)比誰(shuí)好,只有適不適合惠赫。
簡(jiǎn)單的CRUD、試水型的demo項(xiàng)目故黑,用active records模式的ORM框架更好儿咱。
業(yè)務(wù)流程和規(guī)則較多的、成熟的項(xiàng)目改造用data mapper型场晶,其允許將業(yè)務(wù)規(guī)則綁定到實(shí)體混埠。
說(shuō)明:原文中提到的laravel為PHP開(kāi)發(fā)框架。