Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the difference between a ORM, AR, QB, & DM?
    primarykey
    data
    text
    <p>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.</p> <p>An ORM (Object-relational mapping) is a series of tools (loosely or tightly integrated depends) which maps database rows to objects in the application.</p> <p>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.</p> <p>Data mapping (DM) is a type of ORM that is the process of creating data element mappings between two distinct data models.</p> <p>All three claim to work like this:</p> <pre><code>$user = new User(); $user-&gt;name = 'Fred'; $user-&gt;save(); </code></pre> <p>Usually with a User class something like this:</p> <pre><code>class User extends Model { // Specify the database table protected $table = "users"; // Define your fields protected $fields = array( 'id' =&gt; array('type' =&gt; 'int', 'primary' =&gt; true), 'name' =&gt; array('type' =&gt; 'string', 'required' =&gt; true), 'email' =&gt; array('type' =&gt; 'text', 'required' =&gt; true) ); } </code></pre> <p>With this setup you can easily fetch rows without the need to write SQL.</p> <pre><code>// users $users = $user-&gt;fetch(array('id' =&gt; 3)); </code></pre> <p>Some AR classes actually look more like this:</p> <pre><code>$db-&gt;where('id' =&gt; 3); $db-&gt;join('posts', 'posts.user_id = users.id'); $results = $db-&gt;get('users'); </code></pre> <p>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.</p> <p>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.</p> <pre><code>//Fetch the users $results = $db-&gt;select('id, name')-&gt;where('id &gt; 4')-&gt;get('users'); //Set them active while($user = $results-&gt;fetch()) { $user-&gt;active = TRUE; $user-&gt;save(); } </code></pre> <p>So the question is "what is it?", and why don't people agree on these terms?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload