This package provides Active Record pattern implementation.
Supported databases:
| Packages | Build status |
|---|---|
| Microsft SQL Server | |
| MySQL | |
| Oracle | |
| PostgreSQL | |
| SQLite |
- PHP 8.1 - 8.5.
The package could be installed with Composer:
composer require yiisoft/active-recordImportant
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
Defined your active record class (for more information, follow Create Active Record Model guide):
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:
/**
* 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:
// Creating a new record
$user = new User();
$user->username = 'alexander-pushkin';
$user->email = '[email protected]';
$user->save();
// Retrieving a record
$user = User::query()->findByPk(1);
// Read properties
$username = $user->username;
$email = $user->email;- 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.
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.