Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Patterns - Service/Model/Mapper/Dao Examples?

Does someone know some good examples for an PHP Application using following 4 "Layers"

ServiceLayer --> Model --> DataMapper --> DAO

I am not sure if it makes sense but when I use such a design i have to do the following to create a new Record in my Database:

$servcie = new Service(new Mapper(new Dao));
$service->save($data)

The Service is creating a new Data Object and passing it into the Mapper, the Mapper is passing the Data to the provided Dao..

what is the intention to use such constructs ?

Why not simply :

$model = new Model();
$model->save($data)

Model is saving to DB.

like image 970
opHASnoNAME Avatar asked Oct 27 '22 02:10

opHASnoNAME


1 Answers

Ideally the model should have nothing to do with how it is stored or managed. It should be a pure and portable representation of data (ideally, altho often not so in practice). The controller (or a dedicated sub-controller) should be the one handling this functionality for the model.

like image 70
Lior Cohen Avatar answered Nov 15 '22 04:11

Lior Cohen