Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 39 additions & 127 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@
<a href="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/yiisoft" target="_blank">
<img src="https://yiisoft.github.io/docs/images/yii_logo.svg" height="100px" alt="Yii">
</a>
<h1 align="center">Yii ActiveRecord</h1>
<h1 align="center">Yii Active Record</h1>
<br>
</p>

[![Latest Stable Version](https://poser.pugx.org/yiisoft/active-record/v)](https://packagist.org/packages/yiisoft/active-record)
[![Total Downloads](https://poser.pugx.org/yiisoft/active-record/downloads)](https://packagist.org/packages/yiisoft/active-record)
[![codecov](https://codecov.io/gh/yiisoft/active-record/branch/master/graph/badge.svg?token=w4KarhYyEF)](https://codecov.io/gh/yiisoft/active-record)
[![Code Coverage](https://codecov.io/gh/yiisoft/active-record/branch/master/graph/badge.svg)](https://codecov.io/gh/yiisoft/active-record)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Factive-record%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/active-record/master)
[![Static analysis](https://github.com/yiisoft/active-record/actions/workflows/static.yml/badge.svg?branch=master)](https://github.com/yiisoft/active-record/actions/workflows/static.yml?query=branch%3Amaster)
[![type-coverage](https://shepherd.dev/github/yiisoft/active-record/coverage.svg)](https://shepherd.dev/github/yiisoft/active-record)
[![psalm-level](https://shepherd.dev/github/yiisoft/active-record/level.svg)](https://shepherd.dev/github/yiisoft/active-record)

This package provides [ActiveRecord] library.
It is used in [Yii Framework] but is supposed to be usable separately.
This package provides [Active Record pattern](https://en.wikipedia.org/wiki/Active_record_pattern) implementation.

[ActiveRecord]: https://en.wikipedia.org/wiki/Active_record_pattern
[Yii Framework]: https://www.yiiframework.com/
Supported databases:

## Support databases

| Packages | Versions | CI-Actions |
|-----------------------------------------------------|-----------------|------------|
| [[db-mssql]](https://github.com/yiisoft/db-mssql) | **2017 - 2022** |[![Build status](https://github.com/yiisoft/db-mssql/workflows/build/badge.svg)](https://github.com/yiisoft/db-mssql/actions?query=workflow%3Abuild) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fdb-mssql%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/db-mssql/master) [![codecov](https://codecov.io/gh/yiisoft/db-mssql/branch/master/graph/badge.svg?token=UF9VERNMYU)](https://codecov.io/gh/yiisoft/db-mssql)|
| [[db-mysql]](https://github.com/yiisoft/db-mysql) | **5.7 - 8.0** |[![Build status](https://github.com/yiisoft/db-mysql/workflows/build/badge.svg)](https://github.com/yiisoft/db-mysql/actions?query=workflow%3Abuild) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fdb-mysql%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/db-mysql/master) [![codecov](https://codecov.io/gh/yiisoft/db-mysql/branch/master/graph/badge.svg?token=gsKVx3WQt4)](https://codecov.io/gh/yiisoft/db-mysql)|
| [[db-oracle]](https://github.com/yiisoft/db-oracle) | **12 - 21** |[![Build status](https://github.com/yiisoft/db-oracle/workflows/build/badge.svg)](https://github.com/yiisoft/db-oracle/actions?query=workflow%3Abuild) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fdb-oracle%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/db-oracle/master) [![codecov](https://codecov.io/gh/yiisoft/db-oracle/branch/master/graph/badge.svg?token=XGJAFXVHSH)](https://codecov.io/gh/yiisoft/db-oracle)|
| [[db-pgsql]](https://github.com/yiisoft/db-pgsql) | **9.0 - 16.0** |[![Build status](https://github.com/yiisoft/db-pgsql/workflows/build/badge.svg)](https://github.com/yiisoft/db-pgsql/actions?query=workflow%3Abuild) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fdb-pgsql%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/db-pgsql/master) [![codecov](https://codecov.io/gh/yiisoft/db-pgsql/branch/master/graph/badge.svg?token=3FGN91IVZA)](https://codecov.io/gh/yiisoft/db-pgsql)|
| [[db-sqlite]](https://github.com/yiisoft/db-sqlite) | **3:latest** |[![Build status](https://github.com/yiisoft/db-sqlite/workflows/build/badge.svg)](https://github.com/yiisoft/db-sqlite/actions?query=workflow%3Abuild) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fdb-sqlite%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/db-sqlite/master) [![codecov](https://codecov.io/gh/yiisoft/db-sqlite/branch/master/graph/badge.svg?token=YXUHCPPITH)](https://codecov.io/gh/yiisoft/db-sqlite)|
| Packages | Build status |
|------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Microsft SQL Server](https://github.com/yiisoft/db-mssql) | [![Build status](https://github.com/yiisoft/active-record/actions/workflows/db-mssql.yml/badge.svg?branch=master)](https://github.com/yiisoft/active-record/actions/workflows/db-mssql.yml?query=branch%3Amaster) |
| [MySQL](https://github.com/yiisoft/db-mysql) | [![Build status](https://github.com/yiisoft/active-record/actions/workflows/db-mysql.yml/badge.svg?branch=master)](https://github.com/yiisoft/active-record/actions/workflows/db-mysql.yml?query=branch%3Amaster) |
| [Oracle](https://github.com/yiisoft/db-oracle) | [![Build status](https://github.com/yiisoft/active-record/actions/workflows/db-oracle.yml/badge.svg?branch=master)](https://github.com/yiisoft/active-record/actions/workflows/db-oracle.yml?query=branch%3Amaster) |
| [PostgreSQL](https://github.com/yiisoft/db-pgsql) | [![Build status](https://github.com/yiisoft/active-record/actions/workflows/db-pgsql.yml/badge.svg?branch=master)](https://github.com/yiisoft/active-record/actions/workflows/db-pgsql.yml?query=branch%3Amaster) |
| [SQLite](https://github.com/yiisoft/db-sqlite) | [![Build status](https://github.com/yiisoft/active-record/actions/workflows/db-sqlite.yml/badge.svg?branch=master)](https://github.com/yiisoft/active-record/actions/workflows/db-sqlite.yml?query=branch%3Amaster) |

## Requirements

Expand All @@ -41,96 +38,21 @@ The package could be installed with [Composer](https://getcomposer.org):
composer require yiisoft/active-record
```

**Note: You must install the repository of the implementation to use.**

Example:

```shell
composer require yiisoft/db-sqlite
```

## Configure container with database connection

Add the following code to the configuration files, for example:

`config/common/di/db.php`:

```php
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Sqlite\Connection;
use Yiisoft\Db\Sqlite\Driver;

return [
ConnectionInterface::class => [
'class' => Connection::class,
'__construct()' => [
'driver' => new Driver($params['yiisoft/db-sqlite']['dsn']),
],
]
];
```
> [!IMPORTANT]
> See also [installation notes](https://github.com/yiisoft/db/?tab=readme-ov-file#documentation) for `yiisoft/db`
> package.

`config/common/params.php`:
After installing `yiisoft/active-record`, you also need to configure a database connection:

```php
return [
'yiisoft/db-sqlite' => [
'dsn' => 'sqlite:' . dirname(__DIR__) . '/runtime/yiitest.sq3',
]
]
```
1. Configure the connection, follow [Yii Database](https://github.com/yiisoft/db/blob/master/docs/guide/en/README.md)
guide.
2. [Define the Database Connection for Active Record](docs/define-connection.md)

For more information about how to configure the connection, follow [Yii Database](https://github.com/yiisoft/db/blob/master/docs/guide/en/README.md).
## General usage

`config/common/bootstrap.php`:
Defined your active record class (for more information, follow [Create Active Record Model](docs/create-model.md) guide):

```php
use Psr\Container\ContainerInterface;
use Yiisoft\Db\Connection\ConnectionProvider;
use Yiisoft\Db\Connection\ConnectionInterface;

return [
static function (ContainerInterface $container): void {
ConnectionProvider::set($container->get(ConnectionInterface::class));
}
];
```

Be sure to include `bootstrap.php` file using `config-plugin` in `extra` section of `composer.json` (see [yiisoft/config](https://github.com/yiisoft/config)):

```
"extra": {
"config-plugin": {
"bootstrap": "common/bootstrap.php"
}
}
```

Or if you use `config-plugin-file` in `extra` section of `composer.json` as follows:

```
"extra": {
"config-plugin-file": "configuration.php"
}
```

add the line `'bootstrap' => 'common/bootstrap.php',` to `configuration.php` file:

```php
return [
'config-plugin' => [
'bootstrap' => 'common/bootstrap.php',
],
];
```

See other ways to [define the DB connection](docs/define-connection.md) for Active Record.

## Defined your active record class

```php
use Yiisoft\ActiveRecord\ActiveRecord;

/**
* Entity User.
*
Expand All @@ -140,7 +62,7 @@ use Yiisoft\ActiveRecord\ActiveRecord;
* @property string $email
**/
#[\AllowDynamicProperties]
final class User extends ActiveRecord
final class User extends \Yiisoft\ActiveRecord\ActiveRecord
{
public function tableName(): string
{
Expand All @@ -149,49 +71,39 @@ final class User extends ActiveRecord
}
```

For more information, follow [Create Active Record Model](docs/create-model.md).

## Usage

Now you can use the Active Record:
Now you can use the active record:

```php
use App\Entity\User;

// Creating a new record
$user = new User();
$user->set('username', 'yiiliveext');
$user->set('email', '[email protected]');
$user->set('username', 'alexander-pushkin');
$user->set('email', '[email protected]');
$user->save();
```

Using with `ActiveRecord::query()`:

```php
use App\Entity\User;
use Yiisoft\ActiveRecord\ActiveQuery;

$user = User::query()->where(['id' => 1])->one();
// 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](docs/define-connection.md);
- [Create Active Record Model](docs/create-model.md);
- [Define Active Record Relations](docs/define-relations.md);
- [Extending Functionality With Traits](docs/traits/traits.md);
- [Using Dependency Injection With Active Record Model](docs/using-di.md);
- [Optimistic Locking](docs/optimistic-locking.md);
- [Internals](docs/internals.md).
- [Define the Database Connection for Active Record](docs/define-connection.md)
- [Create Active Record Model](docs/create-model.md)
- [Define Active Record Relations](docs/define-relations.md)
- [Extending Functionality With Traits](docs/traits/traits.md)
- [Using Dependency Injection With Active Record Model](docs/using-di.md)
- [Optimistic Locking](docs/optimistic-locking.md)
- [Internals](docs/internals.md)

If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that.
You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).
If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place
for that. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).

## License

The Yii Active Record Library is free software. It is released under the terms of the BSD License.
The Yii Active Record is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.

Maintained by [Yii Software](https://www.yiiframework.com/).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "yiisoft/active-record",
"type": "library",
"description": "Yii ActiveRecord Library",
"description": "Active record pattern implementation",
"keywords": [
"yii",
"active record"
Expand Down
Loading