Skip to content

yiisoft/active-record

Yii

Yii Active Record


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:

Packages Build status
Microsft SQL Server Build status
MySQL Build status
Oracle Build status
PostgreSQL Build status
SQLite Build status

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:

  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):

/**
 * 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

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

Sponsor this project

  •  

Contributors 302