Ok, so everyone has decided (and for good reason) strait SQL is of the devil. This leaves us with many methods of placing a “middle-man” in our code to separate our code from the database. I am now going to spit out all the info I have gathered in the hope someone can set me strait and tell me what I built.
An ORM (Object-relational mapping) is a series of tools (loosely or tightly integrated depends) which maps database rows to objects in the application.
In an AR (Active-Record) is a type of ORM in which a database table or view is wrapped into a class, thus an object instance is tied to a single row in the table.
Data mapping (DM) is a type of ORM that is the process of creating data element mappings between two distinct data models.
All three claim to work like this:
$user = new User();
$user->name = 'Fred';
$user->save();
Usually with a User class something like this:
class User extends Model {
// Specify the database table
protected $table = "users";
// Define your fields
protected $fields = array(
'id' => array('type' => 'int', 'primary' => true),
'name' => array('type' => 'string', 'required' => true),
'email' => array('type' => 'text', 'required' => true)
);
}
With this setup you can easily fetch rows without the need to write SQL.
// users
$users = $user->fetch(array('id' => 3));
Some AR classes actually look more like this:
$db->where('id' => 3);
$db->join('posts', 'posts.user_id = users.id');
$results = $db->get('users');
Ok, now this is where it gets hairy. Everyone and his brother seems to have a different view on what type of code falls where. While most agree that an AR or DM is a type of ORM – but sometimes the lines that tell AR’s from DM’s seem to smear.
I wrote a class that uses a single object ($db) in which you make calls to this object and it handles SQL creation for result saving/fetching.
//Fetch the users
$results = $db->select('id, name')->where('id > 4')->get('users');
//Set them active
while($user = $results->fetch()) {
$user->active = TRUE;
$user->save();
}
So the question is “what is it?”, and why don’t people agree on these terms?
You might as well make up the acronyms ORM, RM DM whatever … it is all just state transferred from one medium to another and wrapped in function/semantics.
Sun Microsystems and Microsoft do it all the time with Java and C#. Let’s take something simple and give it a new name! What a great idea.
If you say ORM .. everyone knows what it is, in its many guises. Your code looks like the Linq stuff though.
No magic, but alot of buzzwords and fuss IMHO.