- skip to content

# Database Abstraction Layer

### User Tools

- Login

### Site Tools

- Recent changes
- Media Manager
- Sitemap

### Sidebar

##### Resources

- Reference Manual
- User Guide
- The Data Dictionary
- ADOdb-xmlschema
- ADOdb Active Record
- Performance Monitoring
- Date/Time Library
- Session Management Library
- Unsupported Addons
- Coding Standards

##### Supported Databases

- Access
- ADO
- CSV
- Firebird
- Frontbase
- IBM DB2
- Informix
- Interbase
- LDAP
- Microsoft SQL Server
- MySQL
- Netezza
- ODBC
- ODBTP
- Oracle
- PDO
- Postgresql
- SAP ADS
- SAP ASE
- SAP Maxdb
- SAP Sqlanywhere
- Sqlite
- Sybase
- Plain Text
- Visual Foxpro

##### Other

- Feature Comparison Matrix
- Connection Matrix
- Include File Guide

v5:activerecord:activerecord_index

Index

# ADOdb Active Record

## Introduction

ADOdb_Active_Record is an Object Relation Mapping (ORM) implementation using PHP. In an ORM system, the tables and rows of the database are abstracted into native PHP objects. This allows the programmer to focus more on manipulating the data and less on writing SQL queries.

The ADODB Active Method library takes advantage of all the functionality of the underlying ADOdb system, from table naming and casing control , mutiple database connections to features such as recordset caching.

Thre are a couple of simple rules that help aid development, especially when developing simple applications in a rapid development environment. Following these rules will avoid additional configuration requirement:

- The table and column names should be a consistent case in case sensitive environments.
- The table and column names should not include special characters or reserved words.
- The name of the table should be a plural, based on the name of class, so for example, a class name of `person`, should be provided by a table name of `persons`, not `people`.
- The unique identifier, and primary key for the record for each table should be called `id`
- The fields in the Active record are bound by the same restraints as the columns in the database, e.g., if a field is declared as NOT NULL in the database, it must follow the same rule in the Active Record.

`ADOdb_Active_Record` is designed upon the principles of the `ActiveRecord` design pattern, which was first described by Martin Fowler. The `ActiveRecord` pattern has been implemented in many forms across the spectrum of programming languages. `ADOdb_Active_Record` attempts to represent the database as closely to native PHP objects as possible.

`ADOdb_Active_Record` maps a database table to a PHP class, and each instance of that class represents a table row. Relations between tables can also be defined, allowing the ADOdb_Active_Record objects to be nested.

ADOdb Active Record functions support the Record Caching functions of ADOdb

## Using ADOdb Active Record

See the tutorial

## Developer Reference

### Active Record Methods

Method | Description
--- | ---
getattributenames | Returns the names in an ADOdb_Active_Record object
save | Updates the object to the database
replace | Inserts or Updates an object
load | Loads an object from the database
find | Finds an object in the database
set | Converts an an array to an ADOdb_Active_Record object
hasmany | Deprecated General class replaced by classHasMany, tableHasMany and tableKeyHasMany
belongsto | Deprecated General class replaced by classBelongsTo, tableBelongsTo and tableKeyBelongsTo
loadrelations | Loads a subset of data in a relationship
lastinsertid | Retrieves the last insert id of a database

### Static Active Record Methods

Method | Description
--- | ---
classhasmany | Sets a one-to-many Active Record relationship
tablehasmany | Sets a one-to-many Active Record table relationship
tablekeyhasmany | Sets a one-to-many Active Record table key relationship
classbelongsto | Sets the parent of a many-to-one Active Record relationship
tablebelongsto | Sets the parent of a many-to-one Active Record table relationship
tablekeybelongsto | Sets the parent of a many-to-one Active Record table key relationship
setdatabaseadaptor | Sets the database connection for an Active Record class

### Associated Connection Methods

Method | Description
--- | ---
getactiverecords | Reads Active Records directly from a connection into an array
getactiverecordsclass | Reads Active Records directly from a connection into objects

### Class Variables

Variable | Description
--- | ---
quotenames | Quotes column names
table | Overrides the associated table name
dbat | Overrides the database connection
where | Sets a default where statement for load()
lockmode | Applies a SELECT lock operation

### Global Variables

Variable | Description
--- | ---
$ADODB_ACTIVE_CACHESECS | Set to true to enable caching of metadata such as field info
$ACTIVE_RECORD_SAFETY | Set to false to disable safety checks. This confirms that the structure of the active record matches the structure of the associated table
$ADODB_ACTIVE_DEFVALS | Use default values of table definition when creating new active record.

——————————————-

Author Credit: Chris Ravenscroft

## Active Records Extended

This is the original one-to-many Active Record implementation. The reason we offer both versions is that the Extended version is more powerful but more complex.

To use, just include adodb-active-recordx.inc.php instead of adodb-active-record.inc.php.

It provides a new function called Find().It also supports loading all relationships using a single query (using joins).

### Extended Active Record Methods

Method | Description
--- | ---
find | finds an ADOdb_Active_Record object in a database

v5/activerecord/activerecord_index.txt · Last modified: 2016/01/18 20:02 by 66.249.75.209

### Page Tools

- Show pagesource
- Old revisions
- Backlinks
- Rename Page
- Back to top