Well, that's why everyone decided (and not in vain) strait SQL from the devil. This leaves us with many methods for placing the “average person” in our code in order to separate our code from the database. Now I'm going to spit out all the information that I have collected, in the hope that someone can tell me and tell me what I built.
ORM (Object Relational Mapping) is a series of tools (tightly or tightly integrated) that map database rows to objects in an application.
In AR (Active-Record), this is an ORM type in which a table or database view is wrapped in a class, so the object instance is bound to one row in the table.
Data Mapping (DM) is an ORM type that is the process of creating data item mappings between two different data models.
All three apply for work as follows:
$user = new User(); $user->name = 'Fred'; $user->save();
Usually with the 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 setting, you can easily get rows without having to write SQL.
// users $users = $user->fetch(array('id' => 3));
Some AR classes are actually more like this:
$db->where('id' => 3); $db->join('posts', 'posts.user_id = users.id'); $results = $db->get('users');
OK, now this is where he gets hairy. Everyone and his brother seem to have a different idea of what type of code falls where. Although most agree that AR or DM is an ORM type, sometimes the lines that report AR from DM appear to be brushstrokes.
I wrote a class that uses a single object ($ db), in which you call this object, and processes the creation of SQL to save / get the result.
//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 do people disagree with these terms?