We are pleased to present the first stable release of Yii Active Record — an implementation of the Active Record pattern for PHP.
The package is built on top of Yii DB, which means it comes with out-of-the-box support for major relational databases: PostgreSQL, MySQL, MSSQL, Oracle, SQLite.
Flexible Model Property Handling
- Dynamic properties — fast prototyping with #[\AllowDynamicProperties]
- Public properties
- Protected properties — encapsulation via getters/setters
- Private properties
- Magic properties
Powerful Relation System
- One-to-one
- One-to-many
- Many-to-one
- Many-to-many — three implementation approaches (junction table, junction model, key array)
- Deep relations — access to related records through intermediate relations
- Inverse relations
- Eager loading — solves the N+1 problem
Extensibility via Traits
ArrayableTrait— convert a model to an arrayArrayAccessTrait— array-style access to propertiesArrayIteratorTrait— iterate over model propertiesCustomConnectionTrait— custom database connectionEventsTrait— event/handler systemFactoryTrait— Yii Factory integration for DIMagicPropertiesTraitandMagicRelationsTrait— magic accessorsRepositoryTrait— repository pattern
Additional Features
- Optimistic Locking — concurrency control using record versioning
- Dependency Injection — support for constructor-based injection
- Flexible configuration — multiple ways to define the database connection
Example
Example AR class:
/**
* 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}}';
}
}
And its usage:
// 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');