Search code examples
phpstandardsdata-layers

Interacting with the database using layers of separation (PHP and WordPress)


I quite often see in PHP, WordPress plugins specifically, that people write SQL directly in their plugins... The way I learnt things, everything should be handled in layers... So that if one day the requirements of a given layer change, I only have to worry about changing the layer that everything interfaces.

Right now I'm writing a layer to interface the database so that, if something ever changes in the way I interact with databases, all I have to do is change one layer, not X number of plugins I've created.

I feel as though this is something that other people may have come across in the past, and that my approach my be inefficient.

I'm writing classes such as

Table
Column
Row

That allow me to create database tables, columns, and rows using given objects with specific methods to handle all their functions:

$column = new \Namespace\Data\Column ( /* name, type, null/non-null, etc... */ );
$myTable = new \Namespace\Data\Table( /* name, column objects, and indexes */ );

\Namespace\TableModel.create($myTable);

My questions are...

Has someone else already written something to provide some separation between different layers?

If not, is my approach going to help at all in the long run or am I wasting my time; should I break down and hard-code the sql like everyone else?

If it is going to help writing this myself, is there any approach I could take to handle it more efficiently?


Solution

  • You seem to be looking for an ORM. Here is one : http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/getting-started-xml-edition.html