Object-Oriented PHP5 Data Mapper ORM

 

Why can’t I do ‘$row->save()’ or ‘$row->update()’ like other database ORMs?

This is a stance that the phpDataMapper project has chosen to take that other similar Data Mapper projects have caved on.  According to the official definition of a Data Mapper (as outlined in theProject Goals page), the mapper is supposed to keep the individual row objects independent and separate from the mapper itself.  This is specifically so your data row objects won’t have any knowledge of the database or underlying data layer.  The row objects sole purpose is to hold data related to the row you are working with, and nothing else.  In phpDataMapper, the row objects are little more than dumb value objects.  Thus, giving the row objects any kind of ’save()’ or ‘update()’ function would force them to have knowledge of the mapper they are tied to, and defeat the whole purpose of the Data Mapper design pattern.

Rows must always be saved by a mapper instead: $mapper->save($row)

This keeps the individual row objects nice and lightweight since all they do is store data.  This is actually ideal and superior to other similar Data Mapper ORM libraries, becuse the individual row objects are much more common and numerous than mappers.  Keeping them as light and simple as possible is more efficient and uses less memory.