GitPedia

Active record

Active Record pattern implementation

From yiisoft·Updated June 23, 2026·View on GitHub·

This package provides [Active Record pattern](https://en.wikipedia.org/wiki/Active_record_pattern) implementation. The project is written primarily in PHP, distributed under the BSD 3-Clause "New" or "Revised" License license, first published in 2018. Key topics include: active-record, activerecord, database, hacktoberfest, yii3.

Latest release: 1.1.0Version 1.1.0
May 14, 2026View Changelog →
<p align="center"> <a href="https://github.com/yiisoft" target="_blank"> <img src="https://yiisoft.github.io/docs/images/yii_logo.svg" height="100px" alt="Yii"> </a> <h1 align="center">Yii Active Record</h1> <br> </p>

Latest Stable Version
Total Downloads
Code Coverage
Mutation testing badge
Static analysis
type-coverage
psalm-level

This package provides Active Record pattern implementation.

Supported databases:

PackagesBuild status
Microsft SQL ServerBuild status
MySQLBuild status
OracleBuild status
PostgreSQLBuild status
SQLiteBuild status

Requirements

  • PHP 8.1 - 8.5.
<details id="require"> <summary>Package dependencies installed automatically.</summary> </details> <details id="suggest"> <summary>Some features require additional packages.</summary>
  • yiisoft/arrays: For \Yiisoft\Arrays\ArrayableInterface support — Yii Array Helper
  • yiisoft/db-sqlite: For SQLite database support — SQLite driver for Yii Database
  • yiisoft/db-mysql: For MySQL database support — MySQL and MariaDB driver for Yii Database
  • yiisoft/db-pgsql: For PostgreSQL database support — PostgreSQL driver for Yii Database
  • yiisoft/db-mssql: For MSSQL database support — Microsoft SQL Server driver for Yii Database
  • yiisoft/db-oracle: For Oracle database support — Oracle driver for Yii Database
  • yiisoft/factory: For factory support — Object factory that is able to resolve dependencies from PSR-11 container
  • yiisoft/event-dispatcher: For events support — PSR-14 event dispatcher
</details>

Installation

The package could be installed with Composer:

shell
composer require yiisoft/active-record

[!IMPORTANT]
See also installation notes for yiisoft/db
package.

After installing yiisoft/active-record, you also need to configure a database connection:

  1. Configure the connection, follow Yii Database
    guide.
  2. Define the Database Connection for Active Record

General usage

Defined your active record class (for more information, follow Create Active Record Model guide):

php
final class User extends \Yiisoft\ActiveRecord\ActiveRecord { public int $id; public string $username; public string $email; public string $status = 'active'; public function tableName(): string { return '{{%user}}'; } }

For fast prototyping you can use dynamic properties by adding #[\AllowDynamicProperties] attribute:

php
/** * Database fields: * @property int $id * @property string $username * @property string $email * @property string $status **/ #[\AllowDynamicProperties] final class User extends \Yiisoft\ActiveRecord\ActiveRecord { }

Now you can use the active record:

php
// Creating a new record $user = new User(); $user->username = 'alexander-pushkin'; $user->email = 'pushkin@example.com'; $user->save(); // Retrieving a record $user = User::query()->findByPk(1); // Read properties $username = $user->username; $email = $user->email;

Documentation

If you need help or have a question, the Yii Forum is a good place
for that. You may also check out other Yii Community Resources.

License

The Yii Active Record is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website
Twitter
Telegram
Facebook
Slack

Contributors

Showing top 12 contributors by commit count.

View all contributors on GitHub →

This article is auto-generated from yiisoft/active-record via the GitHub API.Last fetched: 6/25/2026