PHPackages                             thinkscape/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. thinkscape/activerecord

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

thinkscape/activerecord
=======================

Modern ActiveRecord implementation for PHP 5.4+

417PHP

Since Aug 5Pushed 12y ago2 watchersCompare

[ Source](https://github.com/Thinkscape/ActiveRecord)[ Packagist](https://packagist.org/packages/thinkscape/activerecord)[ RSS](/packages/thinkscape-activerecord/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

ActiveRecord [![Build Status](https://camo.githubusercontent.com/009025e795c1c473c0bd154b5492658bdec1ed7a92d6fc6bddd27c1a84643441/68747470733a2f2f6170692e7472617669732d63692e6f72672f5468696e6b73636170652f4163746976655265636f72642e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/Thinkscape/ActiveRecord) [![Coverage Status](https://camo.githubusercontent.com/c9374c47b0042e7e6f92e4592048233b9e01835c924c834da7a30169026b9ffc/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f5468696e6b73636170652f4163746976655265636f72642f62616467652e706e67)](https://coveralls.io/r/Thinkscape/ActiveRecord)
===========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#activerecord--)

Modern ActiveRecord implementation for PHP 5.4+

It is simple to use, easy to maintain and performant when used together with well-designed userland code. [ActiveRecord is an architectural pattern](https://en.wikipedia.org/wiki/Active_record_pattern) for adding database [CRUD](https://en.wikipedia.org/wiki/CRUD) functionality to domain objects.

[Installation](#installation)[Quick Start](#quick-start)[Documentation](#documentation)---

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

[](#installation)

### Requirements

[](#requirements)

- PHP 5.4.0 or newer
- Database connection using one of the following
    - [Zend\\Db](https://github.com/zendframework/zf2),
    - [Doctrine DBAL 2.3+](https://github.com/doctrine/dbal),
    - [Mongo](http://php.net/manual/en/mongo.installation.php)

### Installation using Composer

[](#installation-using-composer)

1. Inside your app directory run `composer require thinkscape/activerecord:dev-master`
2. Make sure you are using composer autoloader: `include "vendor/autoload.php";`
3. Follow [quick start](#quick-start) instructions.

### Manual installation

[](#manual-installation)

1. Obtain the source code by either:

- cloning git [project from github](https://github.com/Thinkscape/ActiveRecord.git), or
- downloading and extracting [source package](https://github.com/Thinkscape/ActiveRecord/archive/master.zip).

2. Set up class autoloading by either:

- using the provided autoloader: `require "init_autoload.php";`, or
- adding `src` directory as namespace `Thinkscape\ActiveRecord` to your existent autoloader.

3. Follow [quick start](#quick-start) instructions.

Before you jump into quick start, make sure you are using PHP 5.4, you have installed the component into your application and you have included [Composer autoloader](../README.md#installation-using-composer) or the included [autoload\_register.php](../README.md#manual-installation).

### Using with Zend Framework 2

[](#using-with-zend-framework-2)

1. Install source code using one of the above methods.
2. Enable `TsActiveRecord` module in your `config/application.config.php`.
3. Copy `docs/activerecord.global.php.dist` as `config/autoload/activerecord.global.php` inside your application dir.
4. Edit `config/autoload/activerecord.global.php` and assign default db adapter.
5. Read more about [using ActiveRecord with ZF2](docs/zend-framework-2.md)

#### Using with Symfony 2

[](#using-with-symfony-2)

1. Read more about [using ActiveRecord with Symfony 2](docs/zend-framework-2.md)

Documentation
-------------

[](#documentation)

- [Quick Start](#quick-start)
- [Configuration](docs/config.md)
- [CRUD - Create, Read, Update, Delete](docs/CRUD.md)
- [Queries and traversal](docs/queries.md)
- [Persistence methods and DB configuration](docs/persistence.md)
- [Features and add-ons](docs/features.md)
- [Theory and discussion on ActiveRecord pattern](docs/discussion.md)

Quick Start
-----------

[](#quick-start)

### 1) Make your classes ActiveRecords

[](#1-make-your-classes-activerecords)

ActiveRecord is used to add database functionality to your existing model classes. Let's create a simple active record class.

```
use Thinkscape\ActiveRecord;

class Country
{
    use ActiveRecord\Core;
    use ActiveRecord\Persistence\ZendDb;

    protected static $_dbTable    = 'countries';
    protected static $_properties = [ 'name', 'continent', 'population' ];
}
```

> [More info on configuring ActiveRecords](docs/config.md)

### 2) Connect to a database

[](#2-connect-to-a-database)

All [persistence methods](persistence.md) (such as ZendDb, DoctrineDBAL, ...) require a working database connection. We have to create a new connection adapter and configure it with ActiveRecord:

```
use Zend\Db\Adapter\Adapter;

// Create Zend\Db MySQLi adapter
$adapter = new Adapter(array(
   'driver'   => 'Mysqli',
   'database' => 'my_application',
   'username' => 'developer',
   'password' => 'developer-password'
));

// Method 1. Set default adapter for all ActiveRecord instances
Thinkscape\ActiveRecord\Persistence\ZendDb::setDefaultDb($adapter);

// Method 2. Set default adapter for Country class
Country::setDefaultDb($adapter);

// Method 3. Create an instance and assign an adapter to it
$finland = new Country();
$finland->setDb($adapter);
```

> [More info on persistence methods and configuring database](docs/persistence.md)

### 3) Insert, update and delete records

[](#3-insert-update-and-delete-records)

```
// Create new record
$finland = new Country();
$finland->setName('Finland');
$finland->save();      // INSERT INTO country (name) VALUES ("Finland")

// Update
$finland->setName('Maamme');
$finland->save();      // UPDATE country SET name = "Maamme"

// Delete
$finland->delete();    // DELETE FROM country WHERE id = 1
```

> [More info on CRUD operations](docs/CRUD.md)

### 4) Retrieve records from database

[](#4-retrieve-records-from-database)

```
$first = Country::findFirst();
// SELECT * FROM country ORDER BY id ASC LIMIT 1

$countryById = Country::findById(220);
// SELECT * FROM country WHERE id = 220

$countryByName = Country::findOneBy('name', 'Finland');
// SELECT * FROM country WHERE name = "Finland" LIMIT 1

$countryByName = Country::findOne([
    'name' => 'Finland'
]);
// SELECT * FROM country WHERE name = "Finland" LIMIT 1

$allEuropeanCountries = Country::findAll([
    'continent' => 'Europe'
]);
// SELECT * FROM country WHERE continent = "Finland"

$allBigCountries = Country::findAll([
    ['population', 'gt', 30000000]
]);
// SELECT * FROM country WHERE population >= 30000000
```

> [More info on queries and finding records](docs/queries.md)

### 5) Add more features to your class

[](#5-add-more-features-to-your-class)

- [ActiveRecord\\PropertyFilter](docs/property-filter.md)
- ActiveRecord\\AttributeMethods
- ActiveRecord\\Aliasing
- ActiveRecord\\Aggregations
- ActiveRecord\\Associations
- ActiveRecord\\Conversion
- ActiveRecord\\CounterCache
- ActiveRecord\\Callbacks
- ActiveRecord\\Inheritance
- ActiveRecord\\Integration
- ActiveRecord\\Locking\\Optimistic
- ActiveRecord\\Locking\\Pessimistic
- ActiveRecord\\ModelSchema
- ActiveRecord\\NestedAttributes
- ActiveRecord\\Reflection
- ActiveRecord\\Readonly
- ActiveRecord\\ReadonlyAttributes
- ActiveRecord\\Scoping
- ActiveRecord\\Serialization
- ActiveRecord\\Timestamp
- ActiveRecord\\Transactions
- ActiveRecord\\Validations

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/e3098f330726fa848f55dcb74e5c00ce8fb4842d3869c7d4807ca6ff0a291feb?d=identicon)[Thinkscape](/maintainers/Thinkscape)

---

Top Contributors

[![Thinkscape](https://avatars.githubusercontent.com/u/270528?v=4)](https://github.com/Thinkscape "Thinkscape (31 commits)")

### Embed Badge

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

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

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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