yiisoft / active-record
Active record pattern implementation
Fund package maintenance!
Opencollective
yiisoft
Installs: 74 121
Dependents: 16
Suggesters: 0
Security: 0
Stars: 103
Watchers: 22
Forks: 34
Open Issues: 30
pkg:composer/yiisoft/active-record
Requires
- php: 8.1 - 8.4
- yiisoft/db: dev-master
Requires (Dev)
- ext-pdo: *
- friendsofphp/php-cs-fixer: ^3.89.1
- maglnet/composer-require-checker: ^4.7.1
- phpunit/phpunit: ^10.5.58
- rector/rector: ^2.2.3
- roave/infection-static-analysis-plugin: ^1.35
- spatie/phpunit-watcher: ^1.24
- vimeo/psalm: ^5.26.1 || ^6.8.8
- yiisoft/aliases: ^2.0
- yiisoft/arrays: ^3.2
- yiisoft/cache: ^3.1
- yiisoft/db-mssql: dev-master
- yiisoft/db-mysql: dev-master
- yiisoft/db-oracle: dev-master
- yiisoft/db-pgsql: dev-master
- yiisoft/db-sqlite: dev-master
- yiisoft/di: ^1.4
- yiisoft/event-dispatcher: ^1.1
- yiisoft/factory: ^1.3
- yiisoft/test-support: ^3.0.2
Suggests
- yiisoft/arrays: For \Yiisoft\Arrays\ArrayableInterface support
- yiisoft/db-mssql: For MSSQL database support
- yiisoft/db-mysql: For MySQL database support
- yiisoft/db-oracle: For Oracle database support
- yiisoft/db-pgsql: For PostgreSQL database support
- yiisoft/db-sqlite: For SQLite database support
- yiisoft/event-dispatcher: For events support
- yiisoft/factory: For factory support
This package is auto-updated.
Last update: 2025-11-08 04:19:06 UTC
README
Yii Active Record
This package provides Active Record pattern implementation.
Supported databases:
| Packages | Build status |
|---|---|
| Microsft SQL Server | |
| MySQL | |
| Oracle | |
| PostgreSQL | |
| SQLite |
Requirements
- PHP 8.1 - 8.4.
Installation
The package could be installed with Composer:
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:
- Configure the connection, follow Yii Database guide.
- Define the Database Connection for Active Record
General usage
Defined your active record class (for more information, follow Create Active Record Model guide):
/** * Entity User. * * Database fields: * @property int $id * @property string $username * @property string $email **/ #[\AllowDynamicProperties] final class User extends \Yiisoft\ActiveRecord\ActiveRecord { public function tableName(): string { return '{{%user}}'; } }
Now you can use the active record:
// Creating a new record $user = new User(); $user->set('username', 'alexander-pushkin'); $user->set('email', 'pushkin@example.com'); $user->save(); // Retrieving a record $user = User::query()->findByPk(1); // Read properties $username = $user->get('username'); $email = $user->get('email');
Documentation
- Define the Database Connection for Active Record
- Create Active Record Model
- Define Active Record Relations
- Extending Functionality With Traits
- Using Dependency Injection With Active Record Model
- Optimistic Locking
- Internals
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.