Active record pattern

In the software development ActiveRecord is a design pattern for object-oriented software, stores the data in a relational database.

Operation

It was named by Martin Fowler in his book Patterns of Enterprise Application Architecture. An object provides typically interfaces to insert, change and delete according to this pattern. This will relate directly to the underlying (s) table (s).

Active Record is an approach to access data residing in a database. To a database table while a corresponding class is created. An object or an instance of this class then corresponds to a row in the table. Generating a new object, therefore, leads to a new row in the table. Each object is loaded that gets its information from the database. When an object is changed, the corresponding row is changed in the table. The class implements accessor methods for each attribute, and each column of the table.

ActiveRecord is therefore an object which as an adapter (English wrappers) to a row in a database table or database view is used (English View). The adapter this includes database access and business logic for the data. It is basically a Row Data Gateway, which is extended by the business logic and therefore implements both data ( attributes ) and behavior (methods).

Implementations

There are numerous software frameworks that implement the Active Record pattern. If, for example, a database table with the column name products and price out there, and the Active Record pattern is implemented as a class product, then the following pseudo - code creates a new row in the table products with the given attributes.

Product = new product () produkt.name = " Example Product " produkt.preis = 1.99 produkt.save () This corresponds approximately to the SQL command:

INSERT INTO products (name, price) VALUES (' Example Product ', 1.99 ); Conversely, the class product can also be used to load the product:

Product = Produkt.findFirst ("name", "Sample Product") Generating a new product object corresponding to the first product from the product table, the name " sample product " has. The corresponding SQL to could, for example, look like this:

SELECT * FROM products WHERE name = ' Example Product ' LIMIT 1; - MySQL or PostgreSQL References

Pictures of Active record pattern

27986
de