Skip to content

Instantly share code, notes, and snippets.

@kirilkirkov
Last active October 10, 2024 05:58
Show Gist options
  • Save kirilkirkov/65e7ea498195011562118eaeeb1cce6d to your computer and use it in GitHub Desktop.
Save kirilkirkov/65e7ea498195011562118eaeeb1cce6d to your computer and use it in GitHub Desktop.
Active record (Eloquent) vs Data mapper (Doctrine)

In the most of systems need to save data somewhere and in some way. ORM (Object-Relational Mapping), is the way of mapping the system to the database. ORM is the layer between database and application which deals with creating, updating, reading and deleting.

ActiveRecord is type of ORM that looks like that:

class User extends Eloquent {

}

$user = User::find(123);
$user->name = ‘Philip Brown’;
$user->save();

First we inherit Eloquent (which is ActiveRecord type of ORM - just his name is Eloquent) and then i wave save() method which will save our information in the object.

Second type of ORM is DataMapper

DataMapper is ORM style that do not inherit classes like Eloquent (ActiveRecord). For example DataMapper is Doctrine. It looks like this:

$user = new User;
$user->setName(‘Philip Brown’);

EntityManager::persist($user);
EntityManager::flush();

User class is just empty object without inherit. We put them to the EntityManager persist method, and after we call flush() all pushed data to persist() will be saved. This works with transactions.

@kirilkirkov
Copy link
Author

CodeIgniter
CodeIgniter uses a modified version of the Active Record Database Pattern. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action. CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface.

Laravel
The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment