PHPackages                             seiven/php-activerecord - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. seiven/php-activerecord

ActiveLibrary[Database &amp; ORM](/categories/database)

seiven/php-activerecord
=======================

php-activerecord is an open source ORM library based on the ActiveRecord pattern.

v1.2.2(1y ago)0534↓100%MITPHPPHP &gt;=7.1.0

Since Nov 29Pushed 1y agoCompare

[ Source](https://github.com/seiven/php-activerecord)[ Packagist](https://packagist.org/packages/seiven/php-activerecord)[ Docs](http://www.phpactiverecord.org/)[ RSS](/packages/seiven-php-activerecord/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

PHP ActiveRecord - Version 1.0
==============================

[](#php-activerecord---version-10)

[![Build Status](https://camo.githubusercontent.com/b4bba3b41d274d2c386c7c697e0dabb4bd194d8ecb8b2f9abd25519ffeb70c56/68747470733a2f2f7472617669732d63692e6f72672f6a706675656e746573322f7068702d6163746976657265636f72642e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/jpfuentes2/php-activerecord)

by

- [@kla](https://github.com/kla) - Kien La
- [@jpfuentes2](https://github.com/jpfuentes2) - Jacques Fuentes
- [And these terrific Contributors](https://github.com/kla/php-activerecord/contributors)

Introduction
------------

[](#introduction)

A brief summarization of what ActiveRecord is:

> Active record is an approach to access data in a database. A database table or view is wrapped into a class, thus an object instance is tied to a single row in the table. After creation of an object, a new row is added to the table upon save. Any object loaded gets its information from the database; when an object is updated, the corresponding row in the table is also updated. The wrapper class implements accessor methods or properties for each column in the table or view.

More details can be found [here](http://en.wikipedia.org/wiki/Active_record_pattern).

This implementation is inspired and thus borrows heavily from Ruby on Rails' ActiveRecord. We have tried to maintain their conventions while deviating mainly because of convenience or necessity. Of course, there are some differences which will be obvious to the user if they are familiar with rails.

Minimum Requirements
--------------------

[](#minimum-requirements)

- PHP 5.3+
- PDO driver for your respective database

Supported Databases
-------------------

[](#supported-databases)

- MySQL
- SQLite
- PostgreSQL
- Oracle

Features
--------

[](#features)

- Finder methods
- Dynamic finder methods
- Writer methods
- Relationships
- Validations
- Callbacks
- Serializations (json/xml)
- Transactions
- Support for multiple adapters
- Miscellaneous options such as: aliased/protected/accessible attributes

Installation
------------

[](#installation)

Setup is very easy and straight-forward. There are essentially only three configuration points you must concern yourself with:

1. Setting the model autoload directory.
2. Configuring your database connections.
3. Setting the database connection to use for your environment.

Example:

```
ActiveRecord\Config::initialize(function($cfg)
{
   $cfg->set_model_directory('/path/to/your/model_directory');
   $cfg->set_connections(
     array(
       'development' => 'mysql://username:password@localhost/development_database_name',
       'test' => 'mysql://username:password@localhost/test_database_name',
       'production' => 'mysql://username:password@localhost/production_database_name'
     )
   );
});
```

Alternatively (w/o the 5.3 closure):

```
$cfg = ActiveRecord\Config::instance();
$cfg->set_model_directory('/path/to/your/model_directory');
$cfg->set_connections(
  array(
    'development' => 'mysql://username:password@localhost/development_database_name',
    'test' => 'mysql://username:password@localhost/test_database_name',
    'production' => 'mysql://username:password@localhost/production_database_name'
  )
);
```

PHP ActiveRecord will default to use your development database. For testing or production, you simply set the default connection according to your current environment ('test' or 'production'):

```
ActiveRecord\Config::initialize(function($cfg)
{
  $cfg->set_default_connection(your_environment);
});
```

Once you have configured these three settings you are done. ActiveRecord takes care of the rest for you. It does not require that you map your table schema to yaml/xml files. It will query the database for this information and cache it so that it does not make multiple calls to the database for a single schema.

Basic CRUD
----------

[](#basic-crud)

### Retrieve

[](#retrieve)

These are your basic methods to find and retrieve records from your database. See the *Finders* section for more details.

```
$post = Post::find(1);
echo $post->title; # 'My first blog post!!'
echo $post->author_id; # 5

# also the same since it is the first record in the db
$post = Post::first();

# finding using dynamic finders
$post = Post::find_by_name('The Decider');
$post = Post::find_by_name_and_id('The Bridge Builder',100);
$post = Post::find_by_name_or_id('The Bridge Builder',100);

# finding using a conditions array
$posts = Post::find('all',array('conditions' => array('name=? or id > ?','The Bridge Builder',100)));
```

### Create

[](#create)

Here we create a new post by instantiating a new object and then invoking the save() method.

```
$post = new Post();
$post->title = 'My first blog post!!';
$post->author_id = 5;
$post->save();
# INSERT INTO `posts` (title,author_id) VALUES('My first blog post!!', 5)
```

### Update

[](#update)

To update you would just need to find a record first and then change one of its attributes. It keeps an array of attributes that are "dirty" (that have been modified) and so our sql will only update the fields modified.

```
$post = Post::find(1);
echo $post->title; # 'My first blog post!!'
$post->title = 'Some real title';
$post->save();
# UPDATE `posts` SET title='Some real title' WHERE id=1

$post->title = 'New real title';
$post->author_id = 1;
$post->save();
# UPDATE `posts` SET title='New real title', author_id=1 WHERE id=1
```

### Delete

[](#delete)

Deleting a record will not *destroy* the object. This means that it will call sql to delete the record in your database but you can still use the object if you need to.

```
$post = Post::find(1);
$post->delete();
# DELETE FROM `posts` WHERE id=1
echo $post->title; # 'New real title'
```

Contributing
------------

[](#contributing)

Please refer to [CONTRIBUTING.md](https://github.com/jpfuentes2/php-activerecord/blob/master/CONTRIBUTING.md) for information on how to contribute to PHP ActiveRecord.

###  Health Score

27

—

LowBetter than 52% of packages

Maintenance31

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~482 days

Total

2

Last Release

727d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/11b3082a36723ddea8f9e8d2b9d5c3ed0f0ac68b72d4574a58069fa334ff8829?d=identicon)[seiven](/maintainers/seiven)

---

Top Contributors

[![kla](https://avatars.githubusercontent.com/u/27941?v=4)](https://github.com/kla "kla (233 commits)")[![jpfuentes2](https://avatars.githubusercontent.com/u/87429?v=4)](https://github.com/jpfuentes2 "jpfuentes2 (117 commits)")[![greut](https://avatars.githubusercontent.com/u/1388?v=4)](https://github.com/greut "greut (37 commits)")[![koenpunt](https://avatars.githubusercontent.com/u/351038?v=4)](https://github.com/koenpunt "koenpunt (35 commits)")[![Rican7](https://avatars.githubusercontent.com/u/742384?v=4)](https://github.com/Rican7 "Rican7 (33 commits)")[![al-the-x](https://avatars.githubusercontent.com/u/96015?v=4)](https://github.com/al-the-x "al-the-x (15 commits)")[![faulkner](https://avatars.githubusercontent.com/u/22268?v=4)](https://github.com/faulkner "faulkner (7 commits)")[![cvanschalkwijk](https://avatars.githubusercontent.com/u/42311?v=4)](https://github.com/cvanschalkwijk "cvanschalkwijk (7 commits)")[![brianmuse](https://avatars.githubusercontent.com/u/548885?v=4)](https://github.com/brianmuse "brianmuse (6 commits)")[![lfglopes](https://avatars.githubusercontent.com/u/1245319?v=4)](https://github.com/lfglopes "lfglopes (5 commits)")[![anther](https://avatars.githubusercontent.com/u/1376446?v=4)](https://github.com/anther "anther (5 commits)")[![teague2](https://avatars.githubusercontent.com/u/206609?v=4)](https://github.com/teague2 "teague2 (5 commits)")[![NanneHuiges](https://avatars.githubusercontent.com/u/1526794?v=4)](https://github.com/NanneHuiges "NanneHuiges (4 commits)")[![jcs](https://avatars.githubusercontent.com/u/9888?v=4)](https://github.com/jcs "jcs (4 commits)")[![seiven](https://avatars.githubusercontent.com/u/1240664?v=4)](https://github.com/seiven "seiven (4 commits)")[![tomzx](https://avatars.githubusercontent.com/u/188960?v=4)](https://github.com/tomzx "tomzx (3 commits)")[![shmax](https://avatars.githubusercontent.com/u/773172?v=4)](https://github.com/shmax "shmax (3 commits)")[![shaneiseminger](https://avatars.githubusercontent.com/u/843313?v=4)](https://github.com/shaneiseminger "shaneiseminger (2 commits)")[![igorsantos07](https://avatars.githubusercontent.com/u/532299?v=4)](https://github.com/igorsantos07 "igorsantos07 (2 commits)")[![mheijkoop](https://avatars.githubusercontent.com/u/199492?v=4)](https://github.com/mheijkoop "mheijkoop (2 commits)")

---

Tags

ormactiverecord

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/seiven-php-activerecord/health.svg)

```
[![Health](https://phpackages.com/badges/seiven-php-activerecord/health.svg)](https://phpackages.com/packages/seiven-php-activerecord)
```

###  Alternatives

[php-activerecord/php-activerecord

php-activerecord is an open source ORM library based on the ActiveRecord pattern.

1.3k395.8k19](/packages/php-activerecord-php-activerecord)[tommyknocker/pdo-database-class

Framework-agnostic PHP database library with unified API for MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, and Oracle. Query Builder, caching, sharding, window functions, CTEs, JSON, migrations, ActiveRecord, CLI tools, AI-powered analysis. Zero external dependencies.

815.7k](/packages/tommyknocker-pdo-database-class)[bephp/activerecord

micro activerecord library in PHP(only 400 lines with comments), support chain calls and relations(HAS\_ONE, HAS\_MANY, BELONGS\_TO).

1162.1k1](/packages/bephp-activerecord)[spinitron/yii2-dynamic-ar

Extends Yii ActiveRecord for Maria Dynamic Columns

576.7k](/packages/spinitron-yii2-dynamic-ar)[php-patterns/activerecord

php-activerecord is an open source ORM library based on the ActiveRecord pattern.

448.1k](/packages/php-patterns-activerecord)[cycle/active-record

Provides a simple way to work with your database using Active Record pattern and Cycle ORM

671.1k2](/packages/cycle-active-record)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
