-
Notifications
You must be signed in to change notification settings - Fork 7
Home
Maphper - A php ORM using the Data Mapper pattern
A work in progress!
-
Creates database tables on the fly
-
Currently supports database tables but will support XML files, web services even twitter feeds as Data Sources
-
Supports relations between any two data sources
-
Composite primary keys
Maphper takes a simplistic and minimalist approach to data mapping and aims to provide the end user with an intuitive and easy to use API.
The main philosophy of Maphper is that the programmer should deal with sets of data and then be able to manipulate the set and extract other data from it.
This sets up a Data Mapper that maps to the 'blog' table in a MySQL database. Each mapper has a data source. This source could be anything, a database table, an XML file, a folder full of XML files, a CSV, a Twitter feed, a web service, or anything.
The aim is to give the developer a consistent and simple API which is shared between any of the data sources.
It's then possible to treat the $blogs object like an array.
To set up a Maphper object using a Database Table as its Data Source, first create a standard PDO instance:
$pdo = new PDO('mysql:dbname=maphpertest;host=127.0.0.1', 'username', 'password');
Then create an instance of \Maphper\DataSource\DataBase passing it the PDO instance, name of the table and primary key:
$blogSource = new \Maphper\DataSource\Database($pdo, 'blog', 'id');
Finally create an instance of \Maphper\Maphper and pass it the data source:
$blogs = new \Maphper\Maphper($blogSource);
You can loop through all the blogs using:
foreach ($blogs as $blog) {
echo $blog->title . '<br />';
}
Any fields from the database table (or xml file, web service, etc) will be available in the $blog object
Alternatively you can find a specific blog using the ID
echo $blogs[142]->title;
Which will find a blog with the id of 142 and display the title.