MVC - Data Mapping?

I’m not sure what the technique is actually called (think it might be called data mapping) but is where the model still has all the business logic but uses a separate object containing all the database interaction so the model itself doesn’t have to care what type of database server is being used or if any specific SQL commands/syntax have been used.

Probably the main benefit to myself at least would be all the database calls confined to their own object, cutting down on the size of the model class, making it easier for me.

Is it called “data mapping” and is it worth it when dealing with the model of an MVC framework?

Pretty darn close. You’re thinking of data mapper.

http://martinfowler.com/eaaCatalog/dataMapper.html

I think it’s very much worth it, yes. A competing pattern called active record is coming back in vogue (somewhat) thanks to Laravel, but I think data mapper is better.

It definitely sounds like it’s worth going for, the current model class that I’m working on is already 1,231 and is not yet finished, with the rest of the functionality and more validation (I know it’s not got enough validation yet), I wouldn’t be surprised if it ends up being 5,000+ lines long without using the data mapper pattern.

Long term i might also extend PDO to put wrappers around CRUD, which would make it easier to work with especially for things like IN clauses with prepared statements, as I’d just need to send over an array with the values and the sql statement to be prepared

Unless you are learning, why reinvent the wheel? :wink:

Scott