Active record pattern

From Wikipedia, the free encyclopedia
  (Redirected from ActiveRecord)
Jump to: navigation, search



This pattern is commonly used by object persistence tools, and in object-relational mapping (ORM). Typically, foreign key relationships will be exposed as an object instance of the appropriate type via a property.



part = new Part() = "Sample part"
part.price = 123.45



Conversely, the class can be used to query the database:

b = part.find_first("name", "gearbox")




ColdFusion has an open source implementation of the Active Record pattern.

The ColdFusion on Wheels framework has an implementation of the Active Record pattern. It is open source and has the added advantage of requiring no complex configuration.


PHP ActiveRecord is one open-source library designed to fulfil the active record pattern.[2]


  • CakePHP's ORM implements the active record pattern,[4] but as of version 2.x queries return arrays of data, with levels of related data as required. Version 3.0 (still in development as of 2013) is expected to use objects.
  • Symfony's default database layer and ORM "Doctrine" implements active record.
  • CodeIgniter has a query builder it calls "ActiveRecord", but which doesn't implement the Active Record pattern. Instead it implements what the user guide refers to as a modified version of the pattern. The Active Record functionality in CodeIgniter can be achieved by using either CodeIgniter DataMapper library or CodeIgniter Gas ORM library.





The Java language has a new library called ActiveJDBC. ActiveJDBC is an implementation of Active Record design pattern inspired by Ruby on Rails ActiveRecord. ActiveJDBC is lightweight, fast, small and does not require any configuration.

Another library implementing the Active record pattern is jOOQ (for Java Object Oriented Querying). It combines active records with source code generation and a querying DSL similar to SQL allowing for retrieving active records using complex SQL statements.

The Play Framework is a Java web framework which implements the Active Record pattern, using ideas from Ruby on Rails.

Other languages[edit]

There are several open source implementations of the Active Record pattern in other languages, including JavaScript (e.g., ActiveJS's Active Record[6]), Perl (DBIx::Class), ActionScript, Python, Haxe (SPOD[7]), Objective-C[8] and Scala.[9]



Due to the coupling of database interaction and application logic when using the active record pattern, unit testing an active record object without a database becomes difficult. The negative effects on testability in the active record pattern can be minimized by using mocking or dependency injection frameworks to substitute the real data tier with a simulated one.

See also[edit]


  2. ^ "PHP ActiveRecord". 
  3. ^ "Laravel Documentation: Eloquent ORM". 
  4. ^ "CakePHP (most popular PHP framework)". Stanford University Wiki. 
  5. ^ "Ruby Active Record". Retrieved 2013-04-12. 
  6. ^ "ActiveJS Active Record". Retrieved 2011-07-28. 
  7. ^ "SPOD Macros". Retrieved 2013-01-09. 
  8. ^ "Objective Record". Retrieved 14 January 2013. 
  9. ^ "Scala Active Record". Retrieved 4 December 2013. 

External links[edit]